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);
    }
});
有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top