+ 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?
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?
+ 1
Schindlabua You are rightđ
+ 1
Schindlabua Thanks. Can you suggest any other improvements to the code?
+ 1
Depends on what you are going for, looks fine to me :)
Well actually I would prefer `isHidden ? 0 : 1` over `+!state` though.
0
Schindlabua Thanks. I prefer `+!state` since I am operating with binary values.