1

Hello fellow JavaScripters.

I have recently started to mess around with the stampit.js library as a way of studying different inheritance methods in JavaScript.

My question is in regards to my state objects (there are two in my example), each state object is being assigned a different value ("first" and "second"), but my first state object seems to want to copy the value from my second state object, which, as far as I understand, is not the behavior I should expect from stampit.js. Shouldn't my state objects be unique when passed through stamp.create()?

Here is my example:

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"

So what am I missing here?

radiovisual
  • 6,298
  • 1
  • 26
  • 41
Ichigo Kurosaki
  • 317
  • 2
  • 12

1 Answers1

1

At the time of this writing, there is a bug in the stampit.js library.

You can see on the project's github account that the issue was first opened (and closed) here, and then reopened again here.

Seems like they are working on fixing this as we speak, but you are right, you should be getting two different state values for your two different example objects, because state should be "instance safe."

A quote from the library author representing your intended behavior:

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.

So hang tight! This should be fixed very soon!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

UPDATE (JAN 15, 2015): You can 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):

Will be fixed with #43 in version 2.0. See branch v2_0.

radiovisual
  • 6,298
  • 1
  • 26
  • 41