【发布时间】:2012-01-05 08:53:12
【问题描述】:
我在玩 javascript 原型链继承时遇到了这种有趣的行为。
我有一个父类和一个子类
//class parent
function parent(param_1) {
this.param_1 = param_1;
this.getObjWithParam = function(val) {
console.log(this);
console.log("Constructor value in parent class " + this.param_1);
console.log("tguha ----> parent, val " + val);
};
};
//class child
function child(param_1) {
parent.call(this, [ param_1 ]);
};
var childObj = new child(100);
childObj.getObjWithParam(200);
我得到的输出为
**>child**
Constructor value in parent class 100
tguha ----> parent, val 200
我没有在做 //child.prototype = new parent(); 并且仍然继承父类。
谁能帮我解释一下这个场景。
【问题讨论】:
标签: javascript prototypal-inheritance