Are properties shared between instances of a class?


#1

I’m getting this strange behaviour but I’m not sure where the problem is. I’ve been following this guide to create a class and make instances of it but when I try to access one of the properties of an instance it shows the values of all them mixed.

This is what I’ve done:

I’ve created a class with:

Ember.Object.extend({
  content:{}
  addToContent: function(key,value)
  ...
  }
});

then, in a component I’ve imported it and create an instance:

import MyClass from 'path/to/the/class';
   ...
   var x = MyClass.create({});
   x.addToContent("a","1");

   ...

and after that, in a subcomponent I do the following:

 import MyClass from 'path/to/the/class';
       ...
       var y = MyClass.create({});
       y.addToContent("b","1");
    
       ...

and when I show the content of content it has the combination {a:1, b:1}. Shouldn’t x and y be different instances with their own properties? why are they getting mixed?


#2

I’ve solve it by initializing content when calling create, but it keeps looking weird to me. :unamused:


#3

I noodled around with it a bit because I also thought it strange but I guess it makes sense since extend is probably adding to the prototype. I haven’t looked too deeply into how ember implemented oop.

I found a few possibly ways to handle it


#4

Thanks for the alternatives. I’m glad for not being the only one that finds this strange :smiley: