constitute icon indicating copy to clipboard operation
constitute copied to clipboard

Constitute is creating new objects rather than referencing existing

Open rsumilang opened this issue 8 years ago • 1 comments

Consider the following code example:

class Foo {

  bar = 'should be overwritten';

}

let foo1 = constitute(Foo);
let foo2 = constitute(Foo);

foo1.bar = 'i have the new value!';

console.log('foo 1:');
console.log(foo1.bar);

console.log('foo 2:');
console.log(foo2.bar);

In this case, constitute should have cached the instance of Foo when setting foo1. Then foo2 should therefor use the same instance as foo1;

However this is the console output I received:

foo 1:
> i have the new value!
foo 2:
> should be overwritten

rsumilang avatar Mar 18 '16 18:03 rsumilang

Here is another example where we expect to receive the same instance of Car:

class Car {
  message = 'This should be overwritten';
}

@Dependencies(Car)
class Foobar {
  constructor(Car) {
    this.car = Car;
  }
}

let foobar1 = constitute(Foobar);
let foobar2 = constitute(Foobar);

foobar1.car.message = 'i have the new value!';

console.log('foobar 1:');
console.log(foobar1.car.message);

console.log('foobar 2:');
console.log(foobar2.car.message);

Console output:

foobar 1:
> i have the new value!
foobar 2:
> This should be overwritten

rsumilang avatar Mar 18 '16 19:03 rsumilang