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'
Robin Dorbell
  • 1,569
  • 1
  • 13
  • 26
  • 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. – Pointy Apr 14 '16 at 17:15
  • No, this has not worked before. You probably had a `OfflineStorage.prototype.getArticlesByArtNo = function…` previously. – Bergi Apr 14 '16 at 17:17
  • you can also put the function inside the constructor. `this.getArticlesByArtNo = function(params)....` – Hugo S. Mendes Apr 14 '16 at 17:24
  • @Oriol: Interesting dupe target :-) – Bergi Apr 14 '16 at 17:35
  • @Bergi One of the first results that appeared in my search. There are better depetargets, probably – Oriol Apr 14 '16 at 17:42

1 Answers1

-1

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

return {OfflineStorage:OfflineStorage, getArticlesByArtNo:getArticlesByArtNo};
Mahesh Chavda
  • 593
  • 3
  • 9