0

I have a problem that when testing my classes using Jasmine, some properties of an inherited class get garbage initial values despite the fact I initialize them in the parent class.

To be precise: I have a "class"

    function Attributes(attr) { 
       ...  
    }

which is then to be used as a property inside another "class"

function Tag(){
   this.attr = new Attributes();
}

When I instantiate the Tag class

var tag = new Tag();

everything is OK, no garbage is inside tag.attr property. Then I create a child class

function Div(){
    ...
}
Div.prototype = new Tag();

And here it is that the problem appears: in a browser I see that when I instantiate Div class

var div = new Div();

its div.attr property (inherited from Tag) has expected values. But the same code in Jasmine yields that div.attr contains garbage values inside. Note that Attribute class has no uninitialized properties. What it can be and how to get rid of this? For the moment, I have to include this.attr = new Attributes(); in Div class as well, but this approach ruins the idea of inheritance.

P.S. Additional info. As it was mentioned before, in browser the code works very well. The problem appears in Jasmine. For example, this TDD code

describe('inherited objects should have proper values', function(){
    it('creates odd objects', function(){
        var attr = new Attributes(),
            tag = new Tag(),
            div = new Div();
        expect(attr.width).toBe(undefined);
        expect(tag.attr.width).toBe(undefined);
        expect(div.attr.width).toBe(undefined);
    });
});

produces an error only on the last expect:

Expected 15 to be undefined.

while the others are passing perfectly.

Andrew
  • 2,148
  • 5
  • 23
  • 34
  • 1
    possible duplicate of [Why are my JavaScript object properties being overwritten by other instances?](http://stackoverflow.com/questions/13127589/why-are-my-javascript-object-properties-being-overwritten-by-other-instances) – Bergi Feb 07 '14 at 11:46
  • I doubt that this is a duplication. In my case the inheritance is performed not from the same instance as it is in the example you mention. – Andrew Feb 07 '14 at 11:50
  • I can see the same `Div.prototype = new Tag();` structure in your code which is the source of your problem - all divs share only one attribute, which is initialized before the instances. Or what exactly are "garbage values"? – Bergi Feb 07 '14 at 11:54
  • garbage values are not those that one imposes, but some seemingly arbitrary ones. Note, that in the browser it works very well, only in Jasmine it gives this problem. – Andrew Feb 07 '14 at 12:32
  • How are they created? It might help solve the problem. My guess would be that it has to do with DOMready, and when `Div.prototype = new Tag();` is executed. Please provide more details on that `Attribute` class. – Bergi Feb 07 '14 at 12:35
  • They are pure classes that have little to do with DOMReady as well as with any other event. Attribute class is just a collection of other attributes such as "width", "color" etc. and a couple of methods to transform its instance into a string. – Andrew Feb 07 '14 at 12:46
  • That's odd, but without more details (and code) we will not be able to help you with that garbage. – Bergi Feb 07 '14 at 12:50
  • You are right, this is odd. Please, find the Jasmine code added to the main text. – Andrew Feb 07 '14 at 13:35
  • Ok, that's the "garbage". But what's the code for `Div` and `Attribute`? – Bergi Feb 07 '14 at 13:43
  • 1
    After a long analysis it turns out that your first comment was correct. In jasmine, every change like _div.attr.width = 1_ propagated in the Attributes prototype. That means that inheriting by means _Div.prototype = new Tag();_ is wrong and should be doen as you pointed out in that answer: _Tag.call(this);_ and Div.prototype = Object.create(Tag.prototype). Thanks! – Andrew Feb 07 '14 at 15:24

0 Answers0