javascript oop,instanceof和基类 [英] javascript oop, instanceof and base class
问题描述
我正在用JavaScript设计一些类层次结构.到目前为止,它可以正常工作,但是我看不到如何确定对象是否是父类的实例".示例:
I am designing some class hierarchy in JavaScript. It works fine so far, but I can't see how to determine if an object is an "instance" of a parent class. Example:
function BaseObject(name){
this.name = name;
this.sayWhoAmI = function(){
console.log(this.name + ' is a Derivation1 : ' + (this instanceof Derivation1));
console.log(this.name + ' is a Derivation2 : ' + (this instanceof Derivation2));
console.log(this.name + ' is a BaseObject : ' + (this instanceof BaseObject));
};
}
function Derivation1(){
BaseObject.apply(this, ['first derivation']);
}
function Derivation2(){
BaseObject.apply(this, ['second derivation']);
}
var first = new Derivation1();
var second = new Derivation2();
first.sayWhoAmI();
记录以下内容:
first derivation is a Derivation1 : true
first derivation is a Derivation2 : false
first derivation is a BaseObject : false
而second.sayWhoAmI();
记录以下内容:
second derivation is a Derivation1 : false
second derivation is a Derivation2 : true
second derivation is a BaseObject : false
我觉得first
和second
对象都应该说它们是BaseObject
的实例.
I feel like both first
and second
object should have say that they are instances of BaseObject
.
我了解可能无法为此创建JavaScript,但是我想知道是否有一种方法可以实现这一目标.
I understand that JavaScript may not be made for this, but I wonder if there's a way to achieve that.
推荐答案
仅调用Base.apply(...)
不会设置继承. .apply
所做的只是将this
设置为第一个参数,仅此而已.调用父构造函数很重要,但这还不够.
Only calling Base.apply(...)
does not setup inheritance. All that .apply
does is setting this
to the first argument, nothing else. It is important to call the parent constructor, but it is not sufficient.
您需要做的是正确设置原型链.也就是说,您必须将Derivation1.prototype
设置为继承自Base.prototype
的内容.
What you have to do is setting up the prototype chain properly. That is, you have to set Derivation1.prototype
to something that inherits from Base.prototype
.
由于构造函数的每个实例都从构造函数的原型继承,因此您将看到诸如
Since each instance of a constructor function inherits from the constructor functions's prototype, you will see code such as
Derivation1.prototype = new Base();
这是一个坏主意,您已经知道了为什么:Base
需要参数来设置实例特定的属性(在本例中为name
).但是我们并不关心这些属性,因为稍后我们将在子构造函数中使用Base.apply(this, ...)
对其进行初始化.
This is a bad idea and you can already see why: Base
expects arguments to set up instance specific properties (name
in this case). But we don't care about these properties, since we are initialising them later in the child constructor with Base.apply(this, ...)
.
因此,我们所需要的只是一个继承自Base.prototype
的对象,幸运的是,ECMASCript 5定义了一个可以为我们做到这一点的函数(
So all we need is an object inheriting from Base.prototype
and luckily, ECMASCript 5 defines a function which can do that for us (polyfill):
Derivation1.prototype = Object.create(Base.prototype);
这将创建一个继承自Base.prototype
的新对象.现在,由于您用新对象替换了原始原型,因此必须设置constructor
属性,使其正确指向Derivation1
:
This creates a new object inheriting from Base.prototype
. Now, since you replaced the original prototype with a new object, you have to set the constructor
property so that it points correctly to Derivation1
:
Derivation1.prototype.constructor = Derivation1;
下面是一个完整的示例.还可以查看此小提琴和
Below is a complete example. Also have a look at this fiddle and this excellent answer by T.J. Crowder, which explains basically the same issues, but maybe in a better way.
示例:
function BaseObject(name){
this.name = name;
}
// move properties shared by all instances to the prototype!
BaseObject.prototype.sayWhoAmI = function() {
console.log(this.name + ' is a Derivation1 : ' + (this instanceof Derivation1));
console.log(this.name + ' is a Derivation2 : ' + (this instanceof Derivation2));
console.log(this.name + ' is a BaseObject : ' + (this instanceof BaseObject));
};
function Derivation1(){
BaseObject.apply(this, ['first derivation']);
}
Derivation1.prototype = Object.create(BaseObject.prototype);
Derivation1.prototype.constructor = Derivation1;
// some useless method of the child "class"
Derivation1.prototype.someOtherMethod = function() {
return 42;
};
var first = new Derivation1();
first.sayWhoAmI();
这篇关于javascript oop,instanceof和基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!