JavaScript继承问题
这是《Pro JavaScript Design Patterns》一书中的扩展函数,
function extend(subClass, superClass) {
var F = function() {};
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass;
subClass.superclass = superClass.prototype;
if(superClass.prototype.constructor == Object.prototype.constructor) {
superClass.prototype.constructor = superClass;
}
}
我对前 3 行有问题...它创建一个空函数,然后将 F.prototype 设置为 superClass.prototype,这意味着(对于 2 个新的构造函数例如 foo、bar 和 foo 扩展 bar),F.prototype 将有一个构造函数属性: bar 和 proto :Object,或者不是? 第 3 行: subClass.prototype = new F();发生了一些我无法理解的事情。为什么当 F 的 [[Prototype]] 是 Object 时继承发生在这里?
有什么区别
subClass.prototype = new superClass();
前3行和代码执行时 ?我的意思是第一个与第二个的作用相同。
只是添加一个对子类中超类构造函数的调用。 调用的是“className”.superclass.constructor.call(this);
This is the extend function from the book "Pro JavaScript Design Patterns"
function extend(subClass, superClass) {
var F = function() {};
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass;
subClass.superclass = superClass.prototype;
if(superClass.prototype.constructor == Object.prototype.constructor) {
superClass.prototype.constructor = superClass;
}
}
I have problems with the first 3 lines... It creates an empty function and then it sets the F.prototype to superClass.prototype which means (for 2 new constructor functions e.g. foo, bar and foo extends bar) that F.prototype will have a constructor property: bar and proto :Object, or not?
And on line 3: subClass.prototype = new F(); happens something that I cannot understand.. Why the inheritance happens here when F's [[Prototype]] is Object?
What are the differences between the first 3 lines and
subClass.prototype = new superClass();
when the code executes? I mean how The first does the same as the second one.
Just to add there is a call to the superClass constructor in the subClass.
The call is "className".superclass.constructor.call(this);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
注意:
subClass.prototype = new superClass()
将调用构造函数并使用返回的新对象作为原型。这将导致 superClass 本身的属性出现在原型链上(因为该实例是原型对象)。Note:
subClass.prototype = new superClass()
would call the constructor and use the returned new object as the prototype. Which would result in properties onsuperClass
itself would turn up on the prototype chain (since that instance is the prototype object).我们有构造函数:
F
、Super
&子
。我们有对象
f
、super
和 &使用上述构造函数创建的sub
。即
f = new F
、super = new Super
、sub = new Sub
我们知道
f.__proto__ === super.__proto__ === Super.prototype
来自第 2 行从第 3 行我们可以看到
sub.__proto__ === f
&sub.__proto__.__proto__ === Super.prototype
另外,我们在第 5 行
和第 4 行和第 5 行中都有
Sub.superClass === Super.prototype
。 6 我们可以说sub.constructor === Sub
&super.constructor === Super
我们可以在第 3 行第 7 行之前调用
new F
的原因是因为第 7 行设置了f.__proto__.constructor == = Super
其中f.constructor
已经是Sub
。基本上,第 7 行正在清理Super
,并且根本不会影响Sub
,因为您永远不应该在实际代码中调用.__proto__.constructor
。这个特定的函数不会显式地调用
Super
作为函数,而只是确保通过Sub
构造的对象在其链中具有Super.prototype
。We've got constructors:
F
,Super
&Sub
.We've got objects
f
,super
, &sub
that are made using said constructors.i.e.
f = new F
,super = new Super
,sub = new Sub
We know
f.__proto__ === super.__proto__ === Super.prototype
from line 2From line 3 we can see that
sub.__proto__ === f
&sub.__proto__.__proto__ === Super.prototype
Also we have
Sub.superClass === Super.prototype
from line 5.and from line 4 & 6 we can say that
sub.constructor === Sub
&super.constructor === Super
The reason we can call
new F
before on line 3 before line 7 is because line 7 set'sf.__proto__.constructor === Super
where asf.constructor
is alreadySub
. basically line 7 is cleaningSuper
up and shouldn't effectSub
at all because you should never call.__proto__.constructor
in real code.This particular function explicity does not call
Super
as function but only make's sure that objects constructed throughSub
haveSuper.prototype
in their chain.作为替代方案,您可以考虑原型 javascript 库。其中包括继承。
原型 Javascript 库
以下是原型文档中的示例。
这是文档页面
As an alternative, you might consider the prototype javascript library. It includes inheritance.
Prototype Javascript Library
Here is an example from the prototype documentation.
Here is the documentation page