【原创】关于对象+原型+继承(一)

看下面例子:

 1 var a={
 2       x:2,
 3       getNumber:function(z){
 4          return this.x+this.y+z;
 5       }                          
 6 } ;   
 7 
 8 
 9 var b={
10       y:23,
11      __proto__:a;  //此处可在外写成:b.construtor.prototype=a;
12 //表示对象b继承至对象a;__proto__属性为为对象自身隐含属性     
13 } ;  
14 
15 b.getNumber(4);//输出29
//__proto__属性为神秘链接属性,ie无该属性,只可用于学习测试所用,正式编程无法使用

如果调用ES5标准化的实现原型继承的可选方法  var b=Object.create(a);//对象b的原型对象是a(对象b的__proto__属性的原型对象是a)

如下代码:

 

 1 var a={ x:1,cal:function(z){return this.x+this.y+z;}};var b={y:3};  b=Object.create(a);b.y;
 2 //undefined b对象中的属性y被覆盖
 3 
 4 var a={ x:1,cal:function(z){return this.x+this.y+z;}};var b={y:3};  b=Object.create(a);b.x;
 5 //1
 6 
 7 var a={ x:1,cal:function(z){return this.x+this.y+z;}};var b={y:3};  b=Object.create(a);b.cal;
 8 // function a.cal(z),输出a对象的属性
 9 
10 var a={ x:1,cal:function(z){return this.x+this.y+z;}};var b={y:3};  b=Object.create(a);b;
11 //Object {}
12 
13 var a={ x:1,cal:function(z){return this.x+this.y+z;}};var b={y:3};  b=Object.create(a);b.cal(23);
14 //NaN,此时b对象中的属性y被覆盖
15 
16 
17 var a={ x:1,cal:function(z){return this.x+this.y+z;}};var b=Object.create(a);b.y=2;b.cal(23);
18 //26  手动添加对象b的属性y

 未完待续

posted on 2015-12-04 16:53  东渐  阅读(223)  评论(0编辑  收藏  举报

导航