您的位置:首页 > Web前端 > JavaScript

【推荐】关于JS中的constructor与prototype【转】

2011-01-08 11:59 686 查看
最初对js中 object.constructor 的认识:

代码

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中的继承了吧。

摘自:http://blog.csdn.net/niuyongjie/archive/2009/11/15/4810835.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: