简体   繁体   中英

Getter function from defineProperty doesn't return selector

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM