There is such a small piece of code:

if (Rect.__instances__) { Rect.__instances__.push(this); } else { Rect.__instances__ = []; Rect.__instances__ = [].push(this); } 

Essence: if the class has a property __instances__ , then we push this into it. If not, then we first initialize __instances__ an empty array, and then we push this into it.

We need to write this condition in one line of the form:

 Rect.__instances__ ? Rect.__instances__.push(this) : Rect.__instances__ = [].push(this); 

But this code, instead of returning an array with the value of this inside, simply returns one.

Then I tried to do this:

 Rect.__instances__ ? Rect.__instances__.push(this) : Rect.__instances__ = (() => {return []}).push(this); 

To which I gave an error of the form:

Uncaught TypeError: (intermediate value) .push is not a function

Tried it like this:

 Rect.__instances__ ? Rect.__instances__.push(this) : Rect.__instances__ = (() => {new Array()}).push(this); 

The error is the same.

Now I ask for help here. By the way, if someone understood what this code is doing and for what I am writing there is a better solution, like standard library methods, then this approach will also fit :)

PS I added the lambda tag to the question, because here, it seems to be how it comes to this. The fact is that lambda in javascript I googled, and in response I was given only infu associated with the pointer f-mi. Therefore, I ventured to assume that since the matter is most likely in the switch function, then the lambda tag fits here.

  • but why rewrite the code in one line ??? - Grundy
  • @Grundy ugly he looks. I love symmetry: D For that matter, I couldn’t just leave one line in if and 2 in else. And when in one - generally perfect. And yes, I am aware of readability :) - smellyshovel

3 answers 3

Choose It seems the last option is good.

 if (Rect.__instances__) { Rect.__instances__.push(this); } else { Rect.__instances__ = [this]; } 
 Rect.__instances__ ? Rect.__instances__.push(this) : (Rect.__instances__ = [this]); 
 if (!Rect.__instances__) { Rect.__instances__ = []; } Rect.__instances__.push(this); 
 (Rect.__instances__ = Rect.__instances__ || []).push(this); 
  • Yes, the last one is interesting. I would like to know why in the second example in the else-statement brackets? - smellyshovel
  • @smellyshovel, for the same, for what if? ru.stackoverflow.com/a/424351/178988 - Qwertiy
  • But you could write it like that? Rect.__instances__ ? Rect.__instances__.push(this) : Rect.__instances__ = [this]; - smellyshovel
  • @smellyshovel, oh. I thought about the first. In the second, I thought that because of the priorities of operations, but you're right, they are not needed in js. Hmm .. And not only in js . Just very rarely does the assignment fall into this place. And I’ve already attacked a rake with a comma)) - Qwertiy
  • Okay then. In general, the first and third options do not fit unambiguously, since the condition was to write in one line. But for the fourth option, I would naturally mention the decision. I really liked it :) Thank you! - smellyshovel
 (Rect.__instances__ = []).push(this); 

or

 Rect.__instances__ = [this]; 

and push returns the new length of the array, so you observed the unit in the original code version.

  • As usual. I did not think of the simplest. Thanks :) - smellyshovel

 var Rect={}; if (Rect.__instances__) { Rect.__instances__.push("1111"); } else { Rect.__instances__ = new Array("1111"); } console.log(Rect) 

  • Here, too, everything is as it should, thank you, the decision, in theory, is similar to the one above. But I heard that where an array can be declared a literal, it is better not to declare it using the constructor. - smellyshovel
  • one
    Only new Array was missing. - Qwertiy