0

Why can't I access the "inner" functions of this object? I feel like this has worked before.

var OfflineStorage = (function () {

    function OfflineStorage() {
        var db = new Dexie("OfflineStorage");
        db.version(1).stores({
            articles: "ArtNo,Description,Unit"
        });
    }

    function getArticlesByArtNo(params) {
        var regex = new RegExp(params.search, "i");

        return db.articles
            .filter(function (article) { regex.test(article.ArtNo) })
            .toArray();
    }

    return OfflineStorage;

})();

And when I try to access this object like so, I get an error.

var offlinestorage = new OfflineStorage();
offlinestorage.getArticlesByArtNo(); <-- This throws an error 'is not a function'
5
  • Those functions are private local symbols inside the anonymous function. Those are not implicitly made visible as object properties under any circumstances. You can explicitly make getArticlesByArtNo visible by assigning it as a property of the OfflineStorage function you return. Commented Apr 14, 2016 at 17:15
  • No, this has not worked before. You probably had a OfflineStorage.prototype.getArticlesByArtNo = function… previously. Commented Apr 14, 2016 at 17:17
  • you can also put the function inside the constructor. this.getArticlesByArtNo = function(params).... Commented Apr 14, 2016 at 17:24
  • @Oriol: Interesting dupe target :-) Commented Apr 14, 2016 at 17:35
  • @Bergi One of the first results that appeared in my search. There are better depetargets, probably Commented Apr 14, 2016 at 17:42

1 Answer 1

-1

You have to return the functions you want to access from outside like this:

return {OfflineStorage:OfflineStorage, getArticlesByArtNo:getArticlesByArtNo};
Sign up to request clarification or add additional context in comments.

1 Comment

That does not work for the call the OP has shown

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.