javascript Prototype constructor的理解(转)

讲JS的构造的,这个比较清晰,但并不表示必定正确。javascript

这几天一直在思考这个东东,感受比之前理解更深刻了。java

http://blog.csdn.net/chunqiuwei/article/details/22092551c++

function Person(name){
      this.name = name;
      this.showMe=function(){
        alert(this.name);
      }
    }

    Person.prototype.from = function(){
      alert('I come from prototype');
    }

    var father = new Person('js');
    alert(father.constructor);

    function SubPerson(){}

    SubPerson.prototype = father;
    //下面一行,指定继承的对象其构造函数仍为本身,而不是被继承的函数
    //这样一来,就实现了能够用父对象的方法
    //也能够自定义自己的属性和方法
    SubPerson.prototype.constructor = SubPerson;

    var son = new SubPerson();
    son.showMe();
    son.from();

    alert(father.constructor);
    alert(son.constructor);
    alert(SubPerson.prototype.constructor);

URL:c#

  ==========================================================================函数

=================================================================================学习

在学习JS的面向对象过程当中,一直对constructor与prototype感到很迷惑,看了一些博客与书籍,以为本身弄明白了,如今记录以下:this

     咱们都知道,在JS中有一个function的东西。通常人们叫它函数。好比下面的代码spa

function Person(name)   
{   
  alert(name);   
}   
Person('js');//js  

上面的代码中,Person的表现的确跟通常的函数没有什么区别,接着看下面的代码.net

复制代码
代码
function Person(name)   
{   
   this.name=name;   
   this.showMe=function()   
        {   
           alert(this.name);   
        }   
};   
var one=new Person('JavaScript');   
one.showMe();//JavaScript  
复制代码

不少人见到了久违的new操做符,因而就叫Person为“类”,但是又没有关键字class的出现,以为叫“类”有点勉强。因而退而求其次叫 Person为类的构造函数。这些概念好像都没有错,之因此出现这样的状况,多是由于你们都学习了传统的面向对象语言(c++,c#,java等),还 有一种思惟定势吧。为了让javascript也面向对象,要在javascript中找到与传统面向对象语言的影子。但是按照javascript的说 法,function定义的这个Person就是一个Object(对象),并且仍是一个很特殊的对象,这个使用function定义的对象与使用new 操做符生成的对象之间有一个重要的区别。这个区别就是function定义的对象有一个prototype属性,使用new生成的对象就没有这个prototype属性prototype

    prototype属性又指向了一个prototype对象,注意prototype属性prototype对象是两个不一样的东西,要注意区别。在prototype对象中又有一个constructor属性,这个constructor属性一样指向一个constructor对象,而这个constructor对象偏偏就是这个function函数自己。

有点头晕,看下图吧:

 

不相信能够看下面的代码:

复制代码
代码
function Person(name)   
{   
   this.name=name;   
   this.showMe=function()   
        {   
           alert(this.name);   
        }   
};   
  
var one=new Person('js');   
  
alert(one.prototype)//undefined   
alert(typeof Person.prototype);//object   
alert(Person.prototype.constructor);//function Person(name) {...};  
复制代码

上面的代码证实了one这个对象没有prototype属性。

咱们接着看代码:

复制代码
代码
function Person(name)   
{   
   this.name=name;   
   this.showMe=function()   
        {   
           alert(this.name);   
        }   
};   
  
Person.prototype.from=function()   
{   
  alert('I come from prototype.');   
}   
  
var one=new Person('js');   
  
one.showMe();//js,这个结果没有什么好奇怪的   
one.from();//I come from prototype.,这个结果有一点奇怪吧  
复制代码

要解释这个结果就要仔细研究一下new这个操做符了.var one=new Person('js');这个语句执行的过程能够分红下面的语句:

var one={};   
Person.call(one,'js');  

按照《悟透javascript》书中说的,new形式建立对象的过程实际上能够分为三步:

第一步是创建一个新对象(叫A吧);

第二步将该对象(A)内置的原型对象设置为构造函数(就是Person)prototype 属性引用的那个原型对象;

第三步就是将该对象(A)做为this 参数调用构造函数(就是Person),完成成员设置等初始化工做。

其中第二步中出现了一个新名词就是内置的原型对象,注意这个新名词跟prototype对象不是一回事,为了区别我叫它inobj,inobj就指向了函数Person的prototype对象。在person的prototype对象中出现的任何属性或者函数均可以在one对象中直接使用,这个就是javascript中的原型继承了

又头晕了,上图吧!

 

这样one对象经过内置的原型对象inobj就能够直接访问Person的prototype对象中的任何属性与方法了。这也就解释了上面的代码中 为何one能够访问form函数了。由于prototype对象中有一个constructor属性,那么one也能够直接访问constructor 属性。

复制代码
代码
function Person(name)   
{   
   this.name=name;   
   this.showMe=function()   
        {   
           alert(this.name);   
        }   
};   
  
Person.prototype.from=function()   
{   
  alert('I come from prototype.');   
}   
  
var one=new Person('js');   
  
one.showMe();//js,这个结果没有什么好奇怪的   
one.from();//I come from prototype.,这个结果有一点奇怪吧   
alert(one.constructor);//function Person(name) {...}   
alert(Person.prototype.constructor);//function Person(name) {...}  

 

复制代码


 接着看继承是如何实现的。

复制代码
代码
function Person(name)   
{   
   this.name=name;   
   this.showMe=function()   
        {   
           alert(this.name);   
        }   
};   
  
Person.prototype.from=function()   
{   
  alert('I come from prototype.');   
}   
  
function SubPerson()   
{   
}   
SubPerson.prototype=new Person();   
  
var subOne=new SubPerson();   
subOne.from();//I come from prototype.   
alert(subOne.constructor);//function Person(name) {...};   
alert(SubPerson.prototype.constructor);//function Person(name) {...};  
复制代码


继承的实现很简单,只须要把子类的prototype设置为父类的一个(实例化)对象便可。注意这里说的但是对象哦!

 那么经过prototype属性实现继承的原理是什么呢?仍是先看图形说明,而后编写代码进行验证。

 

注意:红色的方框就是把子类与父类连接起来的地方。这个就应该是传说中的prototype链了吧。下面有代码进行验证。

复制代码
代码
function Person(name)   
{   
   this.name=name;   
   this.showMe=function()   
        {   
           alert(this.name);   
        }   
};   
  
Person.prototype.from=function()   
{   
  alert('I come from prototype.');   
}   
var father=new Person('js');//为了下面演示使用showMe方法,采用了js参数,实际多采用无参数   
alert(father.constructor);//查看构造函数,结果是:function Person(name) {...};   
function SubPer()   
{   
}   
SubPer.prototype=father;//注意这里   
SubPer.prototype.constructor=SubPer;   
  
var son=new SubPer();   
son.showMe();//js   
son.from();//I come from prototype.   
alert(father.constructor);//function SubPer(){...}   
alert(son.constructor);//function SubPer(){...}   
alert(SubPer.prototype.constructor);//function SubPer(){...}  
 
复制代码



根据上图的prototype链,还有代码的结果,我想应该明白为何使用prototype可以实现

JS中的继承了吧。

相关文章
相关标签/搜索