简体   繁体   中英

JavaScript Uncaught ReferenceError: ListMgr is not defined

I'm having a huge problem with my javascript code, basically I can't find why is my ListMgr not defined...

I have ListMgr.js loaded (it looks like this for now):

var ListMgr = {
    maxItems : 6,
    currentItems : 1,

    initialize : function (_maxItems, _currentItems) {
        if (_maxItems === undefined) { 
            _maxItems = 6;
        } 
        if (_currentItems === undefined) {
            _currentItems = 1;
        } 
        this.maxItems = _maxItems;
        this.currentItems = _currentItems;
    };
};

Then in my html file I have:

$(document).ready(function(){
    var mgr = new ListMgr();
    mgr.initialize(10, 1);
});

And it throws

Uncaught ReferenceError: ListMgr is not defined.

Any help will be appreciated - I believe everything should be correct here, but I may be mistaken.

ListMgr is an object and not a function so you can't invoke it as new ListMgr() . You probably want to use Object.create() instead.

$(document).ready(function(){
    var mgr = Object.create(ListMgr);
    mgr.initialize(10, 1);
});

There is no need to use new .

$(document).ready(function(){
    ListMgr.initialize(10, 1);
    console.log(ListMgr.maxItems);
    console.log(ListMgr.currentItems);
});
// 10
// 1

I did solve it eventually, my assetic code was incorrect and the file was not actually injected in tag. Silly me.

Thanks for any help anyway!

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