siannone
siannone

Reputation: 6763

Parameter is not passed from child to parent

I'm trying to pass a parameter from the child module to the parent module constructor but for some reasons the parameter is not passed to the parent.

This is the child module:

var Child = (function()
{
    /**
     * @constructor
     */
    var Child = function(offer)
    {
        _Parent.call(this, offer);
    };

    /**
     * Prototype.
     */
    Child.prototype = Object.create(_Parent.prototype);
    Child.prototype.construct = Child;

    return Child;
}());

And the following is the parent:

var _Parent = (function()
{
    /**
     * Contains the offer data.
     *
     * @type {{}}
     */
    var offerData = {};

    /**
     * @construct
     */
    var _Parent = function(offer)
    {
        offerData = offer;
    };

    /**
     * Get the offer price.
     *
     * @param offering Index of the offering of which the price should be returned.
     */
    var getPrice = function(offering)
    {
        if(typeof offering == 'undefined')
        {
            offering = 0;
        }

        return offerData[offering]['Prices']['PriceRow']['TotalPriceInclVAT'];
    };

    /**
     * Prototype.
     */
    _Parent.prototype = {
        construct : _Parent,
        getPrice  : getPrice
    };

    return _Parent;
}());

I'm trying the getPrice() function on the child like this:

var child = new Child(offers);
child.getPrice();

but I receive always Uncaught TypeError: Cannot read property 'undefined' of undefined inside the getPrice function whenever i try to return the data.

Upvotes: 0

Views: 75

Answers (2)

Joseph
Joseph

Reputation: 119857

Are you sure offers isn't undefined?

Another problem is that offerData isn't an instance property but a variable inside a closure where the Parent constructor is defined. When you create a new instance, it will override offerData in the closure, wiping out whatever was defined by the previous instantiation.

It's the same as doing this:

var foo = {};

function Parent(bar){
  foo = bar;
}

Parent.prototype.getFoo = function(){
  return foo;
}

function Child(bar){
  Parent.call(this, bar);
}

Child.prototype = Object.create(Parent.prototype);

var hello = new Parent('Hello');
console.log(hello.getFoo()); // Hello

var world = new Child('World');
console.log(world.getFoo()); // World
console.log(hello.getFoo()); // World... wut???

This can be remedied by putting offerData as an instance property so it attaches per instance. You can always resort to pseudo-privates (prefix _ by convention) if you want to keep the concept of privacy.

var _Parent = function(offer){
  this._offerData = offer;
};

Upvotes: 1

Adi Levin
Adi Levin

Reputation: 5233

It is because you defined _Parent only after you defined Child. You need to first define _Parent and then the Child because the Child uses the parent in the line

Child.prototype = Object.create(_Parent.prototype)

I tested it, and it worked.

Upvotes: 0

Related Questions