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/5/ember.js/4.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中oop的新手,只需要掌握一下这一点 如果我有一个具有属性的类 function myClass(){ this.foo=null; } 然后我使用继承来创建一个子类 myChild.prototype = new myClass(); function myChild(){ alert(this.foo); } 当实例化子类时,我如何设置foo的属性,例如我想提醒“bar”。我不想简单地将“bar”传递给myChild,因为我有一个属性列表要设置,这些属性与my

我是Javascript中oop的新手,只需要掌握一下这一点

如果我有一个具有属性的类

function myClass(){
this.foo=null;
}
然后我使用继承来创建一个子类

myChild.prototype = new myClass(); 

function myChild(){
alert(this.foo);
}
当实例化子类时,我如何设置foo的属性,例如我想提醒“bar”。我不想简单地将“bar”传递给myChild,因为我有一个属性列表要设置,这些属性与myClass中的方法相关,而不是与myChild相关

var newChild = new myChild();

您可以在子构造函数中设置属性,如下所示:

myChild.prototype = new myClass(); 

function myChild(){
   this.foo = "bar";
}
这就是你想要的吗

或者,如果您想灵活处理
foo
在每个实例中包含的内容,您可以在实例化子类之后将其设置为:

 var child = new myChild();
 child.foo = "bar";

参数化您的构造方法

function myClass(foo){
    this.foo=foo;
}
myChild.prototype = new myClass('bar'); 

function myChild(){
    alert(this.foo);
}
var newChild = new myChild();
或:


实际上,您也可以找到这个问题的答案,它类似于其他语言中的继承

如果扩展一个类,子类的构造函数必须接受自己的参数和父类的参数。假设你有:

function Parent(a) {
    this.foo = a;
};

// and 

function Child(b, a) {
    Parent.call(this, a); // executes the parent constructor for this instance
    this.bar = b;
    alert(this.foo);
};

inherits(Parent, Child);
(可以在中找到
继承的实现)

Child
中,您必须调用父类的构造函数并传递参数,类似于在Java或Python中的操作

如果您有许多参数,那么可以使用
参数
对象使事情变得更简单:

function Parent(a, b, c, d) {...};

function Child(e, f) {
   // c and d are parameters for `Child`
   // arguments[0] == e
   // arguments[1] == f
   // all other arguments are passed to Parent, the following
   // creates a sub array arguments[2..n]
   Parent.apply(this, [].slice.call(arguments, 2); 
   /...
}

// later

var child = new Child(e, f, a, b, c, d);


通常,
myChild.prototype=newmyclass()
不是一个好的继承模式,因为大多数情况下,类需要一些参数。这不会对每个实例执行父构造函数,但对所有实例只执行一次。

您的第一个代码段缺少
()
,如果您需要字符串
bar
,则需要引用它。我不确定您要将
bar
传递到哪里。哪个函数应该接受并设置它?子类应该设置它,但您不想将它传递给子类-请详细说明一下好吗?我想在实例化时将foo设置为bar,但不在myChild内的实例化行上。我不能这样做,因为每个对象将传递不同的值这是您想要的<代码>myChild.prototype.foo='bar'感谢felix answer接受,因为它确实回答了我的问题,但我已经意识到,我可以更简单地通过使用一些参数实例化对象,然后在新行上使用额外参数调用方法,而不是尝试一次性完成所有任务。
function Parent(a, b, c, d) {...};

function Child(e, f) {
   // c and d are parameters for `Child`
   // arguments[0] == e
   // arguments[1] == f
   // all other arguments are passed to Parent, the following
   // creates a sub array arguments[2..n]
   Parent.apply(this, [].slice.call(arguments, 2); 
   /...
}

// later

var child = new Child(e, f, a, b, c, d);