Home > Web Front-end > JS Tutorial > body text

Deep understanding of the pitfalls of ' ' in JavaScript_javascript skills

WBOY
Release: 2016-05-16 17:47:22
Original
1085 people have browsed it
1. Add two square brackets
[] []
The square brackets do not have the function of statement blocks, so the two square brackets here are an array. To add two arrays (object types), they must first be converted into value types (basic types).
1. Convert to value type and call valueOf. The valueOf() of [] is still its own
Copy the code The code is as follows:

var arr = [];
arr.valueOf() === arr; // true

2, converted to string, toString of [] It is an empty string
Copy code The code is as follows:

[].toString(); / / ""
String([]) // ""

The result is out. When two empty strings are added, the result is still an empty string. That is, " " here refers to string concatenation rather than number addition.

2. Addition of curly brackets and square brackets
Copy code The code is as follows :

{} []

Note that the braces here are still not object literals, but empty statement blocks. Therefore, it can be removed, which is equivalent to
Copy code The code is as follows:

[]

Note that the two operands that appeared before have become an actual single operand. The " " operator only represents one meaning when there is only one operand: arithmetic addition. That is, there is no meaning of string concatenation here.
The toString() in square brackets is an empty string, which is equivalent to
Copy code The code is as follows:

""

" " represents the arithmetic addition operation. The string is not a number, so it is converted to a numeric type. As mentioned in the previous article, the empty string is converted to a numeric type, which is 0.
Then the final result is 0.

3. Add square brackets and curly brackets
Copy code The code is as follows:

[] {}

Compared with the above, only the order of brackets and parentheses is exchanged. The results are different. After the braces are placed on the right side, the meaning of the braces discussed above is different. The curly braces here are an object literal rather than a statement block.
The operands on both sides of " " are converted into value types: "" and "[object Object]" respectively. At this time " " means string concatenation. That is,
Copy code The code is as follows:

"" "[object Object]"

The result is "[object Object]".

4. Try adding parentheses with them too
A sudden whim! Well, although parentheses have ambiguity, they cannot be used as operands.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template