我正在尝试使用以下代码在JavaScript中定义一个多维对象:
function A(one, two) {
this.one = one;
this.inner.two = two;
}
A.prototype = {
one: undefined,
inner: {
two: undefined
}
};
A.prototype.print = function() {
console.log("one=" + this.one + ", two=" + this.inner.two);
}
var a = new A(10, 20);
var b = new A(30, 40);
a.print();
b.print();
结果是:
one=10, two=40
one=30, two=40
, but I expect
one=10, two=20
one=30, two=40
我做错了什么?
变量inner
是类变量,不是实例变量吗?
JavaScript引擎:Google V8。