js原型理解
?
??? 验证代码:
?
? ? ? 注意:红色的方框就是把子类与父类链接起来的地方。这个就应该是传说中的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(){...}??
?