js原型链继承的几个细节问题

1.怎样实现只继承prototype

先看下面的代码:

function A(){
 this.name="李可可";
 this.age=21;
}
A.prototype.eat=function(){ console.log("I can eat") }
function B(){}
B.prototype=new A;//B继承了A

var cc=new B;
cc.eat();//I can eat
cc.name;//"李可可"

我们可以看到的是,A继承了B的所有属性,那如果我们只想让B继承A.prototype属性,不想要A上面的name和age等一大堆没用的东西,那该怎么办?

有人可能就说了,直接像下面这样不就完了吗:

B.prototype=A.prototype;
var cc=new B;
cc.eat();//I can eat
cc.name;//undefined

哟,好像很完美?继续来看

B.prototype.fuck=function(){console.log("I fuck you!")}
var cc=new B,dd=new A;
cc.fuck();//I fuck you!
dd.fuck();//I fuck you!
//哦买噶的,怎么两个人都学会骂人了
//当子类B的prototype发生变化时也会影响到A的prototype(当然反过来也是),原因也很简单,因为我们让A.prototype指向了B的prototype

 解决办法:

构建一个函数,里面创建一个空的对象,并且让该空对象的prototype指向要继承的父对象,最终返回该对象的实例,代码如下

Object.createPro=function(pro){
  function F(){}
  F.prototype=pro;
  return new F;
}

 我们可以测试来一下:

function A(){
 this.name="李可可";
 this.age=21;
}
A.prototype.eat=function(){ console.log("I can eat") }
function B(){}
B.prototype=Object.createPro(A.prototype);//B只继承了A的prototype属性

var cc=new B;
cc.eat();//I can eat
cc.name;//
B.prototype.fuck=function(){console.log("I fuck you!")}//我们现在改变B的prototype
var dd=new A;
dd.fuck();//报错TypeError
//说明B.prototype的改变并没有影响到A的任何属性

 但是这样做也太麻烦了吧,我们可以直接使用Object自带的一个静态方法create():

function A(){
 this.name="李可可";
 this.age=21;
}
A.prototype.eat=function(){ console.log("I can eat") }
function B(){}
B.prototype=Object.create(A.prototype);//只会继承A的prototype

 在继承的同时也可以给B添加一些特有的属性如下:

function A(){
 this.name="李可可";
 this.age=21;
}
A.prototype.eat=function(){ console.log("I can eat") }
function B(){}
B.prototype=Object.create(A.prototype,{ 
 p: { value: 42, writable: false, enumerable: true }//添加一个属性p,并且是不可写的,但可枚举
});
var pp=new B;
pp.p;//42
pp.name;//undefined
pp.eat();//I can eat
pp.p=666;
pp.p;//42 (不可写)

 其中第二个参数很像Object.defineproperties()可以在里面配置多个属性并且给一些特殊的权限标签

扫描二维码关注公众号,回复: 342364 查看本文章

 当然,想通过此方法继承A的所有属性也是可以的像下面

B.prototype=Object.create(new A);

或者我们可以自己模拟一个,就像上面的Object.createPro方法

2.关于constructor指向问题以及constructor的可枚举性

在第一个问题中,我们使用了Object.create方法实现了不同类之间的继承,然而这样做的话却存在一个问题,如下:

function A(){
 this.name="李可可";
 this.age=21;
}
A.prototype.eat=function(){ console.log("I can eat") }
function B(){}
B.prototype=Object.create(A.prototype);

var cc=new B;
cc.constructor;//A  (这里我们期望的值是B,而实际上变成了A)

 那么如何解决上面的问题呢?

//我们最容易想到的是手动设置constructor属性,像下面这样
B.prototype.constructor=B;

 那么问题又来了,请看下面:

B.prototype.propertyIsEnumerable("constructor");//true (我们设置的constructor属性是可枚举的)

当然我们并不希望是这样,那怎么做呢?

//使用Object.defineProperty或Object.defineProperties方法设置constructor的enumerable为false
Object.defineProperty(B.prototype,"constructor",{
 value:B,
 enumerable:false//不可枚举
});
cc.constructor;//B
B.prototype.propertyIsEnumerable("constructor");//false

 有类似问题的还有使用对象字面量重写类的prototype的时候,就像下面这样

function C(){}
C.prototype={}
var pp=new C;
pp.constructor;//Object  (我们期望的是C)
C.prototype.constructor=C;
C.prototype.propertyIsEnumerable("constructor");//true (同样是可枚举的)
//这里也可以使用上面的方法解决

 当然,还有一种办法就是你不要去重写它,只需要往上面添加属性就行,像下面这样:

function D(){}
D.prototype.x=1;
var gg=new D;
gg.constructor; //D
D.prototype.propertyIsEnumerable("constructor");//false

 诸如此类问题在我的js笔记也有简要的描述(点此进入)

猜你喜欢

转载自cobain-li.iteye.com/blog/2339099