(![]+[])[+[]]... Explain Why This Works

(![]+[])[+[]]... Explain why this works

As @Mauricio commented (![]+[])[+[]] is "f" (the first char of "false"), (![]+[])[+!+[]]) is "a", etc...

How does it work?

Let's examine the first character, 'f':

(![]+[])[+[]]; // 'f'

The first part of the expression—between parentheses—is composed by ![]+[], the first operand of the Addition operator is ![] and it will produce false, because an array object—as any other Object instance—is truthy, and applying the Logical (!) NOT unary operator, it produces the value false, for example.

![]; // false, it was truthy
!{}; // false, it was truthy
!0; // true, it was falsey
!NaN; // true, it was falsey

After it, we have the second operand of the addition, an empty Array, [], this is made just to convert the false value to String, because the string representation of an empty array is just an empty string, is equivalent to:

false+[]; // "false"
false+''; // "false"

The last part, the pair of square brackets after the parentheses, they are the property accessor, and they receive an expression, which is formed by the Unary Plus Operator applied to an empty array again.

What the Unary Plus Operator does is type conversion, to Number, for example:

typeof +"20"; // "number"

One more time, this is applied to an empty Array, and as I said before, the String representation of an Array is an empty string, and when you convert an empty string to Number, it is converted to zero:

+[]; // 0, because
+[].toString(); // 0, because
+""; // 0

Therefore we can "decode" the expression to in some steps:

(![]+[])[+[]];
(false+[])[+[]];
(false+'')[+[]];
(false+'')[0];
('false')[0]; // "f"

Note that accessing characters by using the bracket notation on String values was not part of the ECMAScript 3rd. Edition Specification, (that's why the charAt method existed).

However this kind of "index properties" that represent the characters of a string were standardized on ECMAScript 5, and even before the standardization the feature was available in a good number of browsers (even in IE8 (standards mode)).

Could someone please explain why this works? (Python Iterator)

pet.values() is a list while iter(pet.values()) gives you a list_iterator.
Check following example:

a=[1,2,3]
ia = iter(a)

for i in a:
print(i)
break

for i in a:
print(i)
break

for i in a:
print(i)
break

for i in ia:
print(i)
break

for i in ia:
print(i)
break

for i in ia:
print(i)
break

The output just explains the situation you may have ignored when using iter() func.

What is the proper use of the comma operator?

In your example it serves no reason at all. It is on occasion useful when written as

if(cond)
perror("an error occured"), exit(1) ;

-- then you don't need curly braces. But it's an invitation to disaster.

The comma operator is to put two or more expressions in a position where the reference only allows one. In your case, there is no need to use it; in other cases, such as in a while loop, it may be useful:

while (a = b, c < d)
...

where the actual "evaluation" of the while loop is governed solely on the last expression.

can someone explain why this works in IE browsers?

It appears to be due to the location of the two div elements. In your non-functioning version, this is the html structure (simplified):

<div class="wrapper">
<div id="viewer2" class="viewer">
<img />
</div>
<div class="image-mask"></div>
</div>

In your functioning version where you insert the element, this is what it ends up:

<div class="wrapper">   
<div class="viewer iviewer_cursor" id="viewer2" >
<img />
<div class="iviewer_image_mask"></div>
</div>
</div>

And by moving the mask into the viewer div in your original, I could get it to work in this fiddle.

I believe this makes a difference, since your script is binding to the viewer2 div, so having the mask inside that apparently allows it to be functioning within the context of your script, whereas having it outside does not.

Can you explain how it works?

while(n > 0) {

While the int n is bigger than 0 do the following.

int digit = n % 10;

A new int digit is initialized as n MODULO 10. Modulo returns the remainder of the division n / 10. So for example if n is 21, it will return 1.

int square = digit * digit;
builder.insert(0, square);

A new int square is initialised as the product of digit times itself. The method insert() from class StringBuilder is called with 0 and square as parameters. 0 is the offset and square is the char value to be inserted.

n = Math.floorDiv(n, 10);

Math.floorDiv() returns the largest (closest to positive infinity) integer value that is less than or equal to the algebraic quotient. For example Math.floorDiv(25, 5) will return 5.

return Integer.valueOf(builder.toString());

Finally, you return the value you built before. builder.toString() returns the StringBuilder object as a String. Integer.valueOf(String str) is used to return an Integer object holding the value of the specified String str.



Related Topics



Leave a reply



Submit