+ 1

[SOLVED] JavaScript OOP with functions.

I am trying to build a functional class factory in JavaScript. I succeeded but it looks like a mess. I want to know how I can refer to the instance of the object within the function. If I was using class prototypes I would have used `this` keyword. Here is the code https://code.sololearn.com/W83E62mK52kk/ I want to avoid getInstance() method. Any ideas?

24th Jun 2020, 1:09 PM
Ore
Ore - avatar
5 Answers
+ 2
You are constructing a new object on every call to `getInstance`. Which may be fine, but then of course there is no single "this" you could be referring to. How about simply const instance = { toggleState, trigger }; and then returning that?
24th Jun 2020, 1:18 PM
Schindlabua
Schindlabua - avatar
+ 1
Schindlabua You are right😀
24th Jun 2020, 1:27 PM
Ore
Ore - avatar
+ 1
Schindlabua Thanks. Can you suggest any other improvements to the code?
24th Jun 2020, 1:29 PM
Ore
Ore - avatar
+ 1
Depends on what you are going for, looks fine to me :) Well actually I would prefer `isHidden ? 0 : 1` over `+!state` though.
24th Jun 2020, 1:41 PM
Schindlabua
Schindlabua - avatar
0
Schindlabua Thanks. I prefer `+!state` since I am operating with binary values.
24th Jun 2020, 1:46 PM
Ore
Ore - avatar