grssnbchr
grssnbchr

Reputation: 2974

Weird behavior with OO-Javascript and global variables

I have a global array of objects like so

var data = {};
data.meta = [{..},{..},{..}];

Then I define a Javascript class like so:

var ui = {};
ui.Ruler = function(d) {
    this.d = d;
    this.o = null;
};

ui.Ruler.prototype.render = function () {
    console.log('render');
    return this.o;
};

I can access data.meta from the dev console and it shows me its contents. However, when I initialize an instance of the class and pass it an object of data.meta,

var ruler = new ui.Ruler(data.meta[0]);

The console gives me an error:

Uncaught TypeError: Cannot read property '0' of undefined

Furthermore, data.meta is undefined all of a sudden. How can this be? What am I doing wrong? I want data.meta[0] to be available inside the instantiated ruler object. If I do not run this line, data.meta is still defined. Can somebody explain to me what is happening here?

Upvotes: 0

Views: 46

Answers (1)

dimas
dimas

Reputation: 389

I seem everything is ok. I've tried to run it in FF and Chrome - ok. Here is my fiddle testing link

var ruler = new ui.Ruler(data.meta[0]);

(I can't comment)

Upvotes: 1

Related Questions