【推荐】关于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
代码
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
相关文章推荐
- 【推荐】关于JS中的constructor与prototype【转】
- 【推荐】关于JS中的constructor与prototype【转】
- 【推荐】关于JS中的constructor与prototype【转】
- 【推荐】关于JS中的constructor与prototype【转】
- 【推荐】关于JS中的constructor与prototype【转】
- 【推荐】关于JS中的constructor与prototype【转】
- 【推荐】关于JS中的constructor与prototype【转】
- 【推荐】关于JS中的constructor与prototype【转】
- 【推荐】关于JS中的constructor与prototype【转】
- 关于JS中的constructor与prototype的总结
- 关于JS的prototype和constructor
- 关于JS中的constructor与prototype
- 关于JS中的constructor与prototype
- 关于JS中的constructor与prototype .
- 关于JS中的constructor与prototype
- 关于JS中的constructor与prototype
- 关于JS中的constructor与prototype
- 关于JS中的constructor与prototype的总结
- 关于JS中的constructor与prototype
- 关于JS中的constructor与prototype