function Parent()
{
}
Parent.prototype.hello = "hello";
Parent.prototype.sayHello = function()
{
alert(this.hello);
};
function Child()
{
}
Child.prototype = new Parent();
//扩展属性
Child.prototype.word ="word";
Child.prototype.sayWord = function()
{
alert(this.word);
};
var child = new Child();
child.sayHello();
{
}
Parent.prototype.hello = "hello";
Parent.prototype.sayHello = function()
{
alert(this.hello);
};
function Child()
{
}
Child.prototype = new Parent();
//扩展属性
Child.prototype.word ="word";
Child.prototype.sayWord = function()
{
alert(this.word);
};
var child = new Child();
child.sayHello();
child.sayWord();
缺点是:无法给构造函数传递参数
本文探讨了JavaScript中函数和原型链的继承机制,并通过实例展示了如何使用实例方法实现特定功能。
1472

被折叠的 条评论
为什么被折叠?



