【发布时间】:2010-09-28 23:42:32
【问题描述】:
我看到很多这样的代码:
function Base() {}
function Sub() {}
Sub.prototype = new Base();
但是,如果你这样做:
s = new Sub();
print(s.constructor == Sub);
这是错误的。这似乎让我感到困惑,因为 s 的构造函数确实是 Sub。这样做是否传统/更好?
function Base() {}
function Sub() {}
Sub.prototype = new Base();
Sub.prototype.constructor = Sub;
还是真的不重要?
【问题讨论】:
-
我刚刚通过一个 html 页面作为 alert(s.constructor == Sub) 运行比较,它返回 true。
-
许多框架调整
constructor属性以正确指向子类的构造函数。我有帖子处理上面显示的代码中的这个问题和其他问题。 js-bits.blogspot.com/2010/08/…
标签: javascript inheritance constructor prototype