Reputation: 162
I try to return a selector trough a getter function from an object, which is initialized after its creation. Why is my property "undefined"? Can't wrap my head around this...
http://jsfiddle.net/micka/fBPxG/
HTML:
<div class="current"></div>
JS:
var Slider = {
init: function (config) {
this.config = config;
console.log('this should be the div with a class of current', this.currentSelector)
}
};
Slider.init({
mySelector: $('div')
});
Object.defineProperty(Slider, 'currentSelector', {
get: function () {
return $('.current', this.config.mySelector);
}
});
Upvotes: 0
Views: 82
Reputation: 41
Object.defineProperty(Slider, 'currentSelector', {
get: function () {
return $('.current', this.config.mySelector);
}
});
Object.defineProperty is defined after Slider.init({...})
When Slider.init triggered, the currentSelector attribute hasn't been defined and return undefined.
Solution: Please move up Object.defineProperty
Upvotes: 1