What is "var cc = cc = cc || {};" line to do in Cocos2D?

I'v tried to find a reason to use this line of code

var cc = cc = cc || {}; 

in the Cocos2D JavaScript library, for example, in this place, but I did not find a reasonable reason. The only task in terms of default settings would be nice, but dual purpose? Does anyone know the reason for this?

+4
source share
3 answers

It annoyed me, so I have a game and some tests, and here are my conclusions.

I will show two different script fragments that produce two different results, explaining why someone can use one over the other. The reasons for using either, however, boil down to the encoder and will be based on the effect that they want to perform.

Note. For example, for purposes, I will use actual values, not empty objects.

You can usually see the following example:

 var cc = cc || 1; 

This creates a new variable called cc and gives either the value of the existing (inside the same scope) variable or the default value of 1 . This method does NOT change the original variable, although in practice this will apparently affect the fact that it has changed, since you cannot subsequently refer to the original because it has the same name.

This can be tested using various variable names, for example:

 var aa; alert(aa); var cc = aa || 1; alert(aa); alert(cc); 

( Example ) Here you can see that aa never changes.

Next, we will consider this code:

 var cc = cc = cc || 1; 

This will actually change the original AND variable and create a new local one. Again, it is not easy to see effects, while variables have the same name. However, if we make the same name as above, we can see the real effect:

 var aa; alert(aa); var cc = aa = aa || 1; alert(aa); alert(cc); 

( Example ) This time we can see that aa changing.

In conclusion, you can never see any effect of using one over the other (with the same variable names), but I'm curious what consequences can happen if you can refer somewhere to the original, before the destination, and therefore the choice that will be used, will actually have an effect. I will see if I can find something to show this in action.

+1
source

The code is equivalent to the following:

 var cc; cc = cc || {}; cc = cc; 

which is obviously a mistake.

UPDATE. I have done more research on this topic, and here's an interesting thing:

Whenever you use the var keyword, it creates a new variable in the current scope if it no longer exists in the current scope . Basically this code:

 var cc = 1; function test() { var cc = cc || {}; return cc; } test(); 

always creates {} , no matter what the initial value of cc (in the global scope). In particular, this code:

 var cc = [expression]; 

is equivalent to:

 var cc; cc = [expression]; 

although var cc; creates a new variable only if cc does not exist in the current scope.

UPDATE 2 .. Which operation takes precedence is confusing because in the OP code both = signs do not actually match. The first denotes the declaration of a variable, since the var keyword is before it. The second is the appointment. That's why

 var x = y = z; 

equivalently

 var x; y = z; x = z; 

(note that the var keyword only applies to x ), and

 x = y = z; 

equivalently

 y = z; x = z; 

(note that the operation y=z returns z , which doesn't really matter (this may be y obvious), but it's worth noting)

CONCLUSION: Declaring a variable on the left side is always done before evaluating the right side and assigning the right side to the left.

+2
source

This code is clearly a bug. What the author meant was probably to set both global and internal scope variables to the same value, but this expression always fails. Not only does this not allow you to set an external variable, it resorts to returning {} .

The reason this expression fails is because the declared variables are set to undefined before the assignment. Since this line of code tries to assign a variable with the same name as itself, and since JavaScript first evaluates the internal variables of the scope, cc will always allow the internal self undefined at the time of assignment, which negates the logical evaluation and returns {} .

Another way to see it in code:

 var cc; //cc is set to 'undefined' cc = cc || {}; //which becomes equivalent to: cc = undefined || {}; //which finally evaluates to: cc = {} 
0
source

Source: https://habr.com/ru/post/1469182/


All Articles