简体   繁体   English

Javascript用父属性实例化子类

[英]Javascript instantiating child class with parent properties

I am new to oop in Javascript and just need to get to grips with this. 我是Java的新手,只需要解决这个问题。

If I have a class with properties 如果我有一个带有属性的课程

function myClass(){
this.foo=null;
}

I then use inheritance to create a child class 然后,我使用继承创建一个子类

myChild.prototype = new myClass(); 

function myChild(){
alert(this.foo);
}

How do I set the property of foo when instantiating the child class eg I want to alert 'bar'. 实例化子类时如何设置foo的属性,例如,我想提醒'bar'。 I don't want to simply pass 'bar' to myChild as I have a list of properties to set that are relevant to a method in myClass and not myChild. 我不想简单地将“ bar”传递给myChild,因为我要设置与myClass而不是myChild中的方法相关的属性列表。

var newChild = new myChild();

You could just set the property in the child's constructor like so: 您可以像这样在孩子的构造函数中设置属性:

myChild.prototype = new myClass(); 

function myChild(){
   this.foo = "bar";
}

Is that what you want? 那是你要的吗?

Or if you want to be flexible about what foo contains in each instance you could just set it right after instantiation of the child class: 或者,如果您想对每个实例中包含的foo保持灵活性,可以在实例化子类后立即设置它:

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

Parameterize your construction method. 参数化您的构造方法。

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

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

or: 要么:

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

You can actually find the answer to this question also in my answer to previous question and it is similar to inheritance in other languages. 实际上,您也可以在我对上一个问题的回答中找到该问题的答案 ,它类似于其他语言中的继承。

If you extend a class, the constructor function of the child class has to accept its own arguments and the one for the parent class. 如果扩展一个类,则子类的构造函数必须接受其自己的参数而父类则应接受该参数。 So assuming you have: 因此,假设您有:

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);

(the implementation of inherits can be found in this answer ). inherits的实现可以在此答案中找到)。

Inside Child you have to call the construtor of the parent class and pass the parameters, similar how you do it in Java or Python. Child内部,您必须调用父类的构造函数并传递参数,类似于在Java或Python中的操作方式。

If you have many parameters, then you can make use of the arguments object, to make things a bit easier: 如果您有许多参数,则可以使用arguments对象,使事情变得容易一些:

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);

In general, myChild.prototype = new myClass(); 通常, myChild.prototype = new myClass(); is not a good inheritance pattern as most of the time, classes expect some arguments. 在大多数情况下,类不是一个好的继承模式,类需要一些自变量。 This won't execute the parent constructor for each instance but only once for all instances. 这不会为每个实例执行父构造函数,而是为所有实例执行一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM