简体   繁体   中英

How can I solve “Cannot call method ”addL“ of undefined”

I was trying to make by myself a "library" for my personal project which uses local storage for nearly everything and I got this error.

The Browser I am using is Google Chrome on the last version. It says no error line on the console and the error is:

TypeError: Cannot call method 'addL' of undefined.

JavaScript

function local (title) {
    var storeTitle = title;
    this.addL = function(lString) {
        var storeText = lString;
        localStorage.setItem(storeTitle, storeText);
    };
    this.removeL = function() {
        localStorage.removeItem(storeTitle);
    };
    this.getL = function () {
        localStorage.getItem(storeTitle);
    };
};

I can't find the error and when I google Cannot call method ... of undefined it shows a lot of pages but with different content, not the one I'm looking for. I found from Google Maps API to jQuery API.

I learned this "way" from another question here StackOverflow.

You are missing the new keyword. So try this:

new local("locally").addL("stored")

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