第五章 原型

  • 在JavaScript中,所有函数都会拥有一个 prototype 的属性,默认初始值为空对象。
  • 可以在相关的原型对象中添加新的方法和属性,甚至可以用自定义对象来完全替换掉原有的原型对象。
  • 通过某个构造器函数来new一个对象时,这些对象就会自动拥有一个指向 prototype 属性的 __proto__链接,通过它可以访问相关原型对象的属性。
  • 对象自身属性的优先级要高于其原型对象的同名属性。
  • 扩展内建对象不是一个好主意,如果必须要采用的话一定要谨慎。
  • 当我们对原型对象执行完全替换时,可能会触发原型链某种异常,prototype.constructor属性是不可靠的。
  • Best Practice:当我们重写某对象的prototype时,重置相应的constructor属性是一个好习惯。
  • 每个对象都会有一个isprototype()方法,这个方法会告诉我们当前对象是否是另一个对象的原型。
  • 神秘的__proto__链接。

    枚举属性

      

参考链接:JavaScript for...in循环

第六章 继承

 //6.1.1 原型链示例
//6.1.2 将共享属性迁移到原型中去
//6.2 只继承于原型
function Shape(){ }
Shape.prototype.name = 'shape';
Shape.prototype.toString = function(){
return this.name;
}
function TwoDShape(){ }
TwoDShape.prototype = Shape.prototype;
TwoDShape.prototype.constructor = TwoDShape;
TwoDShape.prototype.name = '2D shape'; function Triangle(side, height){
this.side = side;
this.height = height;
}
Triangle.prototype = TwoDShape.prototype;
Triangle.prototype.constructor = Triangle;
Triangle.prototype.name = 'Triangle';
Triangle.prototype.getArea = function(){
return this.side * this.height / 2;
}
//临时构造器 new F()
function Shape(){ }
Shape.prototype.name = 'shape';
Shape.prototype.toString = function(){
return this.name;
}
function TwoDShape(){ }
var F = function(){};
F.prototype = Shape.prototype;
TwoDShape.prototype = new F();
TwoDShape.prototype.constructor = TwoDShape;
TwoDShape.prototype.name = '2D shape'; function Triangle(side, height){
this.side = side;
this.height = height;
}
var F = function(){};
F.prototype = TwoDShape.prototype;
Triangle.prototype = new F();
Triangle.prototype.constructor = Triangle;
Triangle.prototype.name = 'Triangle';
Triangle.prototype.getArea = function(){
return this.side * this.height / 2;
} //6.3 uber——子对象访问父对象的方式
function Shape(){}
Shape.prototype.name = 'shape';
Shape.prototype.toString = function(){
var result = [];
if (this.constructor.uber){
result[result.length] = this.constructor.uber.toString();
}
result[result.length] = this.name;
return result.join(', ');
}
function TwoDShape(){ }
var F = function(){};
F.prototype = Shape.prototype;
TwoDShape.prototype = new F();
TwoDShape.prototype.constructor = TwoDShape;
TwoDShape.uber = Shape.prototype;
TwoDShape.prototype.name = '2D shape'; function Triangle(side, height){
this.side = side;
this.height = height;
}
var F = function(){};
F.prototype = TwoDShape.prototype;
Triangle.prototype = new F();
Triangle.prototype.constructor = Triangle;
Triangle.uber = TwoDShape.prototype;
Triangle.prototype.name = 'Triangle';
Triangle.prototype.getArea = function(){
return this.side * this.height / 2;
} // 6.4 将继承部分封装成函数
function extend(Child, Parent){
var F = function(){};
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
Child.uber = Parent.prototype;
}
extend(TwoDShape, Shape);
extend(Triangle, TwoDShape); //6.5 属性拷贝
function extend2(Child, Parent){
var p = Parent.prototype;
var c = Child.prototype;
for (var i in p){
c[i] = p[i];
}
c.uber = p;
} //6.6 小心处理引用拷贝
//6.7 对象之间的继承
function extendCopy(p){
var c = {};
for(var i in p){
c[i] = p[i];
}
c.uber = p;
return c;
} //6.8 深拷贝
function deepCopy(c, p){
var c = c || {};
for (var i in p){
if (typeof p[i] === 'object'){
c[i] = (p[i].constructor === Array) ? [] : {};
deepCopy(c[i], p[i])
}else{
c[i] = p[i];
}
}
return c;
}
//6.9 object(),Douglas Crockford为我们提出了一个建议,既可以用object()
//函数来接受父对象,并返回一个以该对象为原型的新对象。
function object()(o){
var n;
function F(){}
F.prototype = o;
n = new F();
n.uber = o;
return n;
} //6.10 原型继承于属性拷贝的混合使用
function objectPlus(o, stuff){
var n;
function F(){};
F.prototype = o;
n = new F();
n.uber = o; for(var i in stuff){
n[i] = stuff[i];
}
return n;
} //6.11 多重继承
function multi(){
var n = {}, stuff, j = 0, len = arguments.length;
for(j = 0; j < len; j++){
stuff = arguments[j];
for(var i in stuff){
n[i] = stuff[i];
}
}
return n;
} //6.12 寄生式继承
var twoD = {
name : '2D shape',
dimensions : 2
}
function triangle(s, h){
var that = object(twoD);
that.name = 'Triangle';
that.getArea = function(){
return this.side * this.height / 2
};
that.side = s;
that.height = h;
return that;
} //6.13 构造器借用
function Shape(id){
this.id = id;
}
Shape.prototype.name = 'shape';
Shape.prototype.toString = function(){
return this.name;
}
function Triangle(){
Shape.apply(this, arguments);
}
Triangle.prototype = new Shape();
Triangle.prototype.name = 'Triangle';
//消除双重继承
function Shape(id){
this.id = id;
}
Shape.prototype.name = 'shape';
Shape.prototype.toString = function(){
return this.name;
}
function Triangle(){
Shape.apply(this, arguments);
}
function extend2(Child, Parent){
var p = Parent.prototype;
var c = Child.prototype;
for (var i in p){
c[i] = p[i];
c.uber = p;} }
extend2(Triangle, Shape);
Triangle.prototype.name = 'Triangle';

最新文章

  1. 尝试使用Memcached遇到的狗血问题
  2. Sqrtx
  3. 集成ShareSDK,分享成功后QQ和空间回调不执行的可能原因
  4. 鸟哥的linux私房菜学习笔记 __ 命令与文件的搜寻
  5. Mysql_mysql多个TimeStamp设置
  6. CCF真题之Z字形扫描
  7. model.addAttribute(&quot;student&quot;,student)——渲染
  8. c语言知识点总结(摘自head first c)
  9. Eyeshot Ultimate 学习笔记(2)
  10. java笔记11之二维数组
  11. Android provider authorities冲突
  12. poj 3190 Stall Reservations 贪心 + 优先队列
  13. 为什么Intent传递对象的时候必须要将对象序列化呢?
  14. 【原创】Spring MVC项目搭建(使用Java配置)
  15. 多线程面试题系列(8):经典线程同步 信号量Semaphore
  16. 几个常用的文本处理shell 命令:find、grep、sort、uniq、sed、awk
  17. 用premake5创建lua532工程
  18. API知识点总结
  19. TODO 软件测试68题
  20. [COCI2015]COCI

热门文章

  1. 判断对象当中有没有某一个属性(AS,JS,Java语言比较)
  2. 电脑上不了网,但是能登录QQ 问题解决方案
  3. spring boot系列(一)spring boot 初识
  4. Eclipse进行Debug时断点上有一个斜杠,并且debug没有停在断点处
  5. Xcode真机报错clang: error: linker command failed with exit code 1 (use -v to see invocation)
  6. centos 7.3镜像制作
  7. Qt qss问题总结
  8. 【ABAP系列】SAP ABAP 带有参数的AMDP的创建
  9. Leetcode之动态规划(DP)专题-877. 石子游戏(Stone Game)
  10. RFC3550中文