为什么jordan
的属性不是Human
类?不应该说Coder.prototype = new Human;
足以让所有Coder
类继承Human
类的所有属性?JavaScript中的继承不起作用
它是否与将函数定义为赋值有关?
var Human = function() {
var hi = function() {
alert('hi');
};
return {
name : 'dan',
sayHi : hi
};
};
var dan = new Human();
var Coder = function() {
var code = function() {
alert('1010101');
};
return {
code : code
};
};
Coder.prototype = new Human;
Coder.prototype.constructor = new Coder;
var jordan = new Coder();
console.log(jordan);
是否有一个原因,你想隐藏瓦尔码嗨? –
只需要说JavaScript中的继承“很好运行”。你的代码不起作用。 – pkuderov
@pkuderov伟大的贡献。 – user2727253