Javascript中的prototype与继承

通常来说,javascript中的对象就是一个指向prototype的指针和一个自身的属性列表javascript创建对象时采用了写时复制的理念。
只有构造器才具有prototype属性,原型链继承就是创建一个新的指针,指向构造器的prototype属性。
prototype属性之所以特别,是因为javascript时读取属性时的遍历机制决定的。本质上它就是一个普通的指针。
 
构造器包括:
1.Object
2.Function
3.Array
4.Date
5.String
下面我们来举一些例子吧
 

 

  1. //每个function都有一个默认的属性prototype,而这个prototype的constructor默认指向这个函数
  2. //注意Person.constructor 不等于 Person.prototype.constructor. Function实例自带constructor属性
  3. functionPerson(name){
  4. this.name = name;
  5. };
  6. Person.prototype.getName =function(){
  7. returnthis.name;
  8. };
  9. var p =newPerson("ZhangSan");
  10. console.log(Person.prototype.constructor===Person);// true
  11. console.log(p.constructor===Person);// true ,这是因为p本身不包含constructor属性,所以这里其实调用的是Person.prototype.constructor

 

 

运行一下 »

我们的目的是要表示
1.表明Person继承自Animal
2. 表明p2是Person的实例 
我们修改一下prototype属性的指向,让Person能获取Animal中的prototype属性中的方法。也就是Person继承自Animal(人是野兽)  
 

 

  1. functionPerson(name){
  2. this.name = name;
  3. };
  4. Person.prototype.getName =function(){
  5. returnthis.name;
  6. };
  7. var p1 =newPerson("ZhangSan");
  8. console.log(p.constructor===Person);// true
  9. console.log(Person.prototype.constructor===Person);// true
  10.  
  11. functionAnimal(){}
  12. Person.prototype =newAnimal();//之所以不采用Person.prototype = Animal.prototype,是因为new 还有其他功能,最后总结。
  13. var p2 =newPerson("ZhangSan");
  14. //(p2 -> Person.prototype -> Animal.prototype, 所以p2.constructor其实就是Animal.prototype.constructor)
  15. console.log(p2.constructor===Person);// 输出为false ,但我们的本意是要这里为true的,表明p2是Person的实例。此时目的1达到了,目的2没达到。

运行一下 »

但如果我们这么修正

 

Person.prototype = new Animal();
Person.prototype.constructor = Person; 
这时p2.consturctor是对了,指向的是Person,表示p2是Person类的实例,但是新问题出现了。此时目的2达到了,目的1没达到。
目的1和目的2此时互相矛盾,是因为此时prototype表达了矛盾的两个意思,
1表示父类是谁
2作为自己实例的原型来复制 
因此我们不能直接使用prototype属性来表示父类是谁,而是用getPrototypeOf()方法来知道父类是谁。 
 

 

  1. Person.prototype =newAnimal();
  2. Person.prototype.constructor=Person
  3. var p2 =newPerson("ZhangSan");
  4. p2.constructor//显示 function Person() {}
  5. Object.getPrototypeOf(Person.prototype).constructor//显示 function Animal() {}

 

 
就把这两个概念给分开了 ,其实通过使用 hasOwnProperty()方法,什么时候访问的是实例属性,什么时候访问的是原型属性就一清二楚了

new做了哪些事情?

当代码var p = new Person()执行时,new 做了如下几件事情:

创建一个空白对象

创建一个指向Person.prototype的指针

将这个对象通过this关键字传递到构造函数中并执行构造函数。

具体点来说,在下面这段代码中,

 

  1. Person.prototype.getName =function(){}
如果我们通过

 

 

 

  1. var person =newPerson();
  2.  
  3. 其实类似于
  4.  
  5. var person =newObject();
  6.  
  7. person.getName =Person.prototype.getName;

 

 

因此通过person.getName()调用方法时,this指向的是这个新创建的对象,而不是prototype对象。

 这其实在给现有函数加上新功能的情况下会用到,我们可以这么扩展现有的方法:

 

 

  1. //function myFunc 的写法基本上等于 var myFunc = new Function();
  2.  
  3. function myFunc (){
  4. }
  5.  
  6. myFunc =function(func){
  7.   //可以在这里做点其他事情
  8. returnfunction(){
  9.      //可以在这里做点其他事情
  10. return func.apply(this,arguments);
  11. }
  12. }(myFunc)

 

 

 也可以在Function.prototype方法里直接通过this来访问上面代码的myFunc所指向的对象

 

 

  1. function myFunc (){
  2. }
  3.  
  4. if(!Function.prototype.extend){
  5. Function.prototype.extend =function(){
  6. var func =this;
  7.  
  8. returnfunction(){
  9. func.apply(this,arguments);
  10. }
  11. }
  12. };
  13.  
  14. var myFunc = myFunc.extend();

 

 

总结一下

如果采用Person.prototype  = Animal.prototype来表示Person继承自Animal, instanceof方法也同样会显示p也是Animal的实例,返回为true.

之所以不采用此方法,是因为下面两个原因:

1.new 创建了一个新对象,这样就避免了设置Person.prototype.constructor = Person 的时候也会导致Animal.prototype.constructor的值变为Person,而是动态给这个新创建的对象一个constructor实例属性。这样实例上的属性constructor就覆盖了Animal.prototype.constructor,这样Person.prototype.constructor和Animal.prototype.contructor就分开了。

2.Animal自身的this对象的属性没办法传递给Person

但是像下面这样直接调用构造函数又可能失败,或者产生其他影响。

 

  1. Person.prototype =newAnimal();

 

 为了避免这种情况,所以我们引入了一个中间函数。所以正确的做法应该是

 

 

  1. Person.prototype =(funtion(){
  2.  
  3.   function F(){};
  4.  
  5.   F.prototype =Animal.prototype
  6.  
  7.   returnnew F();
  8.  
  9. })()

 

 

猜你喜欢

转载自zaixianshouce.iteye.com/blog/2355657