You can test your level of knowledge in this area.
At the end of the question is my analysis of the topic with reference to the original blogger’s articles and comments, and everyone is competing to get the answer.
The quiz:
1:
1 && 3
2:
1 && "foo" || 0
3:
1 || "foo" && 0
4:
(1,2,3)
5:
x = {shift:[].shift};
x.shift(); 3: x.length;
6:
{foo:1}[0]
7:
[true, false][ true, false]
8:
'52'.split(' ')[0]
9:
a: b: c: d: e: f: g: 1, 2, 3, 4, 5;
10:
{a: 1, b: 2}[["b"]]
1
"b" 45
12:
{a:{b:2}}
13:
(function(){}())
14:
[1,2,3,4,5][0..toString.length]
15:
({} 'b' > {} 'a')
16:
Number.prototype.x = function(){ return this === 123; };
(123).x() ;
17:
Array(2).join()
18:
vars: var vars = vars;
19:
{ foo = 123 }
20:
x = 1; (function(){return x; var x = 2;}())
2
delete [].length;
22:
RegExp.prototype.toString = function() {return this.source};
/3/-/2/;
23:
{break;4;}
24:
'foo' == new function(){ return String('foo'); };
25:
'foo'.split('') []
Analysis:
1: #1. //3: 1 is true, and the && operation continues to execute the expression on the right, and the result is 3
2: #2. //"foo": logical operator, Same as above, when the operation reaches "foo", the expression has been successful and the expression on the right side of || is no longer executed
3: #3. //1: 1 is converted to bool and is true, and is returned directly, no Then execute
4: #4. //3: Always return the last value
5: #5. //0: When x executes the shift() method, x will have the length attribute. And the returned value is 0
6: #6. //[0] : Two expressions, return the result of the last expression
7: #7. //true : Operate on bool, the result is 1 or 0 is [true,false][1,0], and this formula always takes the last one, [true,false][0]
8: #8. //6: .Operator priority Level greater than operator, '52'.split('')->['5','2']; take [0] to get 5; get 6
9: #9. //5: Ignore the front All :x gets: a:1,2,3,4,5 Result: 5
10: #10. //SyntaxError. Maybe you will be surprised. Isn’t this data in json format? Why does it report an error? I was very surprised at first. In fact, this is not a JavaScript object, but a statement execution of a block-level structure, because there is no assignment statement. When executed as an ordinary statement, {a:1;b:2} is correct. Or a={a:1,b:2}
11: #11. //"b45": Addition of string and number always returns string
12: #12. //2: It's just two statement blocks. It's actually the same situation as question 9 a:b:2, return 2
13: #13. //undefined: The anonymous empty function executes itself, because the return statement is not displayed , automatically returns undefined.
14: #14. //2: This is very interesting. Let’s talk about it in two parts, let’s talk about 0..toString first. If you add "." after an integer, then JavaScript will think it is the point of the floating point number, not the point of the attribute call. If you add a dot after the floating point number, then JavaScript will think it is the point of the attribute call, because JavaScript cannot distinguish this time. What does the user want to do? So 0.. becomes a calling attribute. (This is my guess) At this time, 0. will be converted into an object and its toString function will be called. If you directly use 0.toString, a syntax error will occur. You can test 1.1.toString;.0.toString, etc., they are all callable. Let’s talk about the result of 0..toString.length: 1. Why is it 1? The result returned by calling the length attribute of the function is the number of formal parameters of the function. The default number of formal parameters of the number.toString function in JavaScript is 1. Therefore, the result of [1,2,3,4,5][1] is 2.
15: #15. //true: {} "b" object and string are added -> "[object Object]b", and then logical comparison is performed, "b" > "a". Please do not test {} "b" directly. You will
get NaN. Why? If so, execute the {} statement block first, and then execute "b". The result will naturally be NaN.
16: #16. //false Strict comparison, object on the left, number on the right, type mismatch.
17: #17. //'','': The array is converted into a string after using join, but it is an empty array, so the above result is obtained.
18: #18. //undefined: They are all salted fish, no matter how they turn around, they are still salted fish. They are all undefined, and of course they are still undefined in the end.
19: #19. //123: Block statement execution. She has a little bit to do with her partner, Shenma.
20: #20. //undefined: It is said that every time JavaScript introduces a block scope, it will scan the "var" in the block scope and set the variable value with var life to undefined, regardless of whether it has been done before. Class declaration, including function body.
21: #21. //false: Delete the hair. Can this be deleted? [delete only returns false when a property can not be deleted.]
Reference: http://perfectionkills.com/understanding-delete/ The article makes it very clear that some properties of built-in functions cannot be deleted. , similar to arguments,
length, local variables of functions (function(){ var a = 1; return delete a })(), etc.
22: #22. //1: First modify the toString function on the prototype chain of the regular expression to return the text form of the current regular expression instance object. Then the strings are subtracted. At this time, they will be automatically converted into numbers for calculation.23: #23. // SyntaxError: break statements can only be placed in loops and switch branch statements 24: #24. //false: Borrowing Damian Wielgosik’s very classic explanation (new function(){ return String('foo') ; }).toString() != 'foo'
25: #25. //"f,o,o": After converting the string into an array, add it to the array and then convert it into a string. You can find two Try adding arrays, borrowing Damian Wielgosik’s explanation
Just consider e.g. [1, 2] [3, 4] and see how arrays are casted to string.