Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:继承原型而不重新定义构造函数_Javascript_Oop - Fatal编程技术网

Javascript:继承原型而不重新定义构造函数

Javascript:继承原型而不重新定义构造函数,javascript,oop,Javascript,Oop,我在理解javascript继承和构造函数时遇到了问题,尽管有像和这样的手册 我想创建一个原型和继承它的孩子。原型有一个构造函数(或者换句话说,是一个函数)。 我希望孩子们继承这个构造函数,而不是为每个孩子重新定义构造函数。父类的构造函数将要做很多事情,我不想在子类中重复这些代码。甚至构造函数的参数列表也可能会更改,在这种情况下,我只想在父构造函数中更改它们,而不是在每个子构造函数中 下面是一个在JSFIDLE上工作的示例(另请参见): 首先是运行测试的序言和一些保存键入的函数(跳过): 这更符

我在理解javascript继承和构造函数时遇到了问题,尽管有像和这样的手册

我想创建一个原型和继承它的孩子。原型有一个构造函数(或者换句话说,是一个函数)。 我希望孩子们继承这个构造函数,而不是为每个孩子重新定义构造函数。父类的构造函数将要做很多事情,我不想在子类中重复这些代码。甚至构造函数的参数列表也可能会更改,在这种情况下,我只想在父构造函数中更改它们,而不是在每个子构造函数中

下面是一个在JSFIDLE上工作的示例(另请参见):

首先是运行测试的序言和一些保存键入的函数(跳过):

这更符合我的代码要求,但不是行为。这种情况会导致在子项中未定义This.url:

var child = function(){
}
child.prototype = Object.create(proto.prototype);
child.prototype.constructor = proto.prototype.constructor
child.prototype.sayHello = sayHello2
runtest()
这也不起作用,因为它导致sayHello2也被用于proto而不是child

var child = proto.prototype.constructor
child.prototype = Object.create(proto.prototype);
child.prototype.sayHello = sayHello2
runtest()

需要一段时间才能理解重新定义构造函数的含义。您要做的是在实例化子对象时调用父对象的构造函数

所以你不想这样,也就是重新分配
this.url=url
,对吗

var child = function(url, anotherFancyArg){
    this.url = url;
    this.anotherFancyArg = anotherFancyArg;
}
改为这样做:

var child = function(url, anotherFancyArg){
    proto.apply(this, arguments);
}
child.prototype = Object.create(proto.prototype); // you inherit from parent's prototype
child.prototype.constructor = child; // but you instantiate the child object
现在,您可以使用以下引用访问子实例中的url和其他FancyArg:
this.url
this.anotherFancyArg
,例如

var c = new child('google.com', 'abc');
console.log(c.url); // you get google.com here
还有一件事我注意到了。这是错误的:

child.prototype = Object.create(proto.prototype);
child.prototype.constructor = proto.prototype.constructor;
改为这样做:

var child = function(url, anotherFancyArg){
    proto.apply(this, arguments);
}
child.prototype = Object.create(proto.prototype); // you inherit from parent's prototype
child.prototype.constructor = child; // but you instantiate the child object

对不起,我把问题读了三遍。。。我仍然无法理解它。@第四,我认为OP想要的是不必在子构造函数中重新分配
this.url=url
。谢谢,这确实是我需要的。“参数”和apply方法是我所需要的。
child.prototype = Object.create(proto.prototype); // you inherit from parent's prototype
child.prototype.constructor = child; // but you instantiate the child object