I am getting my feet wet with ES6 and implementing design patterns in Javascript for live projects. I have the following example snippet of code which throws an error saying that the this keyword evaluates to undefined:
export let LifecycleFactoryObj = (() => {
this.myCollection = new WeakSet();
//.... More here
this.add = function (opts) {
let tempObject = new MyCollection(opts);
this.myCollection.add(tempObject);
if (this.myCollection.has(tempObject)) {
return tempObject;
}
return null;
};
})();
The error that is thrown is:
undefined.lifecycles = new WeakSet(); ^ TypeError: Cannot set property 'lifecycles' of undefined
Looks like this inside the IIFE is being evaluated to undefined but I can't seem to trace why. I thought ES6 had scoped the this keyword correctly when using let for variable instantiation.
Thanks!