function Parent()
{
}Parent.prototype.hello = "Hello";
Parent.prototype.sayHello = function()
{
alert(this.hello);
}function Child()
{
}Child.prototype = new Parent();
Child.prototype.world = "World";
Child.prototype.sayWorld() = function()
{
alert(this.World);
}上面实现继承是没问题的,我做一些修改为什么就不可以了, 修改如下:
function Parent(username)
{
this.username = username;
}
Parent.prototype.hello = "Hello";
Parent.prototype.sayHello = function()
{
alert(this.hello);
}function Child()
{
}
Child.prototype = new Parent("mmmm");
Child.prototype.world = "World";
Child.prototype.sayWorld() = function()
{
alert(this.World);
}在我实现继承的时候
var child = new Child("1111", "2222");
就会报错, 求高手解答!
{
}Parent.prototype.hello = "Hello";
Parent.prototype.sayHello = function()
{
alert(this.hello);
}function Child()
{
}Child.prototype = new Parent();
Child.prototype.world = "World";
Child.prototype.sayWorld() = function()
{
alert(this.World);
}上面实现继承是没问题的,我做一些修改为什么就不可以了, 修改如下:
function Parent(username)
{
this.username = username;
}
Parent.prototype.hello = "Hello";
Parent.prototype.sayHello = function()
{
alert(this.hello);
}function Child()
{
}
Child.prototype = new Parent("mmmm");
Child.prototype.world = "World";
Child.prototype.sayWorld() = function()
{
alert(this.World);
}在我实现继承的时候
var child = new Child("1111", "2222");
就会报错, 求高手解答!
{
alert(this.World);
}
这里写错了,去掉括号
Child.prototype.sayWorld = function()
{
alert(this.World);
}
{
alert(this.World);
}
这里的大写变成小写
我自己仔细分析了下是问题是出在this.test = function(){.........}这里,
在new Parent()的时候,test函数方法也会new一次,它在new 的时候是调用Parent的
Parent.prototype.constructor的方法而不是它本身的constructor, 而Parent.prototyep.constructor就包含了test,所以一直就无限链下去了