When using the store
keyword, it has a slightly different meaning compared to using this
. If you treat store.add
like a regular function, for example passing it as an argument to another function, using store
will make the function refer back to the original store
, while using this
would make it refer to the global object.
A tradeoff of using the add
method is that it will always reference the object currently identified by the variable store
, not necessarily the object originally identified by that variable. To benefit from both approaches, one can utilize an immediately-invoked function expression:
var store = (function () {
var store = {
... // same code as previously defined, but 'store' now refers to
// the local variable which remains unchanged
};
return store;
})();
It's possible that the code author didn't have a specific use case in mind and simply found it clearer to reference store
as store
even within its methods.