stampit.js `state` 对象不是实例安全的
stampit.js `state` objects not instance-safe
大家好JavaScript各位。
我最近开始摆弄 stampit.js 库,作为研究 JavaScript 中不同继承方法的一种方式。
我的问题是关于我的 state
对象(在我的示例中有两个),每个状态对象都被分配了不同的值("first" 和 "second"),但是我的第一个状态对象似乎想从我的第二个状态对象复制值,据我所知,这不是我应该从 stampit.js 期望的行为。通过 stamp.create()
时,我的状态对象不应该是唯一的吗?
这是我的例子:
var stamp = require('stampit')();
var state = { x: { y : null } };
state.x.y = "first";
var example1 = stamp.create(state);
state.x.y = "second";
var example2 = stamp.create(state);
// These are the same, but shouldn't they be different?
console.log(example1.a.b); // prints "second"
console.log(example2.a.b); // prints "second"
那么我在这里错过了什么?
在撰写本文时,stampit.js 库中存在错误。
您可以在项目的 github 帐户上看到问题是第一个 opened (and closed) here, and then reopened again here。
好像他们正在努力解决这个问题,但你是对的,你应该为你的两个不同的示例对象获得两个不同的状态值,因为状态应该是 "instance safe."
引用库作者的话代表您的预期行为:
State prototypes are deep-copied at instance creation time, which
means that if you create an instance, then change the state prototype,
then create a new instance, the new instance will have different state
than the first instance.
This is by design.
坚持住!这应该很快就能解决!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
更新(2015 年 1 月 15 日): 您可以 follow the progress of the 2.0 branch here, which claims to be working on these issues. Below is a quote from one of the project maintainers (source:
大家好JavaScript各位。
我最近开始摆弄 stampit.js 库,作为研究 JavaScript 中不同继承方法的一种方式。
我的问题是关于我的 state
对象(在我的示例中有两个),每个状态对象都被分配了不同的值("first" 和 "second"),但是我的第一个状态对象似乎想从我的第二个状态对象复制值,据我所知,这不是我应该从 stampit.js 期望的行为。通过 stamp.create()
时,我的状态对象不应该是唯一的吗?
这是我的例子:
var stamp = require('stampit')();
var state = { x: { y : null } };
state.x.y = "first";
var example1 = stamp.create(state);
state.x.y = "second";
var example2 = stamp.create(state);
// These are the same, but shouldn't they be different?
console.log(example1.a.b); // prints "second"
console.log(example2.a.b); // prints "second"
那么我在这里错过了什么?
在撰写本文时,stampit.js 库中存在错误。
您可以在项目的 github 帐户上看到问题是第一个 opened (and closed) here, and then reopened again here。
好像他们正在努力解决这个问题,但你是对的,你应该为你的两个不同的示例对象获得两个不同的状态值,因为状态应该是 "instance safe."
引用库作者的话代表您的预期行为:
State prototypes are deep-copied at instance creation time, which means that if you create an instance, then change the state prototype, then create a new instance, the new instance will have different state than the first instance.
This is by design.
坚持住!这应该很快就能解决!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
更新(2015 年 1 月 15 日): 您可以 follow the progress of the 2.0 branch here, which claims to be working on these issues. Below is a quote from one of the project maintainers (source: