【发布时间】:2013-03-28 17:41:09
【问题描述】:
这是我的代码,B类继承A类:
function A() {
this.msg = 'meuahah';
A.prototype.foo = function() {
alert(this.msg);
}
}
function B() {
A.call(this);
B.prototype.bar = function() {
A.prototype.foo();
}
}
a = new A();
a.foo(); // alerts 'meuahah'
b = new B();
b.bar(); // alerts 'undefined'
为什么 b.bar() 不显示“meuahah”?
【问题讨论】:
-
哇...为什么我们每次调用构造函数时都要为原型设置东西?
-
当你尝试 alert(a.msg); 时它会起作用;
标签: javascript class inheritance