Another javascript quiz (code set)

Source: Internet
Author: User

You can test your knowledge in this area.
After the question is answered, I will refer to the original blog post and comment on the question analysis, and everyone is competing.
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:
{Fool = 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 ('') + []

Resolution:

1: #1. // 3: 1 is true, and the & Operation continues to execute the expression on the right. The result is 3.
2: #2. // "foo": logical operator. Like above, when the operation reaches "foo", the expression has been successfully executed and will not be executed | expression on the right
3: #3. // 1: 1 is converted to bool and returns directly without executing
4: #4. // 3: The total returns the last value.
5: #5. // 0: After x executes the shift () method, x will have the length attribute and the returned value is 0.
6: #6. // [0]: two expressions. The result of the last expression is returned.
7: #7. // true: For bool + operations, the result is 1 or 0. Therefore, [true, false] [1, 0]. This formula always takes the last one, [true, false] [0].
8: #8. // 6 :. the operator priority is greater than the ++ operator, '52 '. split ('')-> ['5', '2']; obtain [0] for 5; ++ for 6
9: #9. // 5: ignore all the preceding values: x. The result is as follows: a: 1, 2, 4, and 5. The result is 5.
10: #10. // SyntaxError. Maybe you will be surprised, isn't this json-format data? Is it possible to report an error if it is swollen? I was surprised at the beginning. In fact, this is not a javascript object, but a block-level structure statement execution because there is no value assignment statement. When executed as a normal statement, {a: 1; B: 2} is correct. Or a = {a: 1, B: 2}
11: #11. // "b45": string and number addition operations, always return string
12: #12. // 2: It is only two statement blocks. Question 9th is actually a situation where a: B: 2, and 2 is returned.
13: #13. // undefined: the undefined function is automatically returned because no return statement is displayed.
14: #14. // 2: This is very interesting. 0 .. toString first. If you add ". ", javascript will regard this as a floating point, not a property call point. If you add a value after the floating point, javascript will regard it as a property call, because javascript cannot tell you what you want to do. So 0 .. is the call property. (This is an even guess) then 0. is converted into an object and its toString function is called. If 0. toString is used directly, a syntax error occurs. You can test 1.1.toString;. 0. toString and so on, all of which can be called. 0 more .. toString. result of length: 1. Why is it a pinch? The result returned by calling the length attribute of the function is the number of form parameters of the function. The default value of javascript is number. the toString function has 1 parameter. Therefore, the result of [1, 2, 3, 4, 5] [1] is 2.
15: #15. // true: {} + "B": Add the object and the string-> "[Object] B", and then perform logical comparison. "B"> "". Please do not directly test {} + "B" so that you will

If so, execute the {} block first, and then execute + "B". The result is naturally NaN.

16: #16. // The type of the object on the left and the number on the right does not match.
17: #17. // '','': The array is converted to a string after join, but it is an empty array. Therefore, the preceding result is obtained.
18: #18. // undefined: It's all salted fish. They are all undefined, and finally undefined.
19: #19. // 123: BLOCK statement execution. There is a little wool relationship with the object 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, whether or not there have been such declarations, including the function in vitro.
21: #21. // false: Delete the hair. Can this be deleted? [Delete only returns false when a property can not be deleted .]

Reference: some properties of the built-in function cannot be deleted, similar to arguments,

Length, function local variables (function () {var a = 1; return delete a}) (), etc.

22: #22. // 1: First modify the toString function on the prototype chain of the Regular Expression and return the text form of the current regular expression instance object. Then, the string is subtracted and is automatically converted to number for calculation. 23: #23. // SyntaxError: The break statement can only be placed in the loop and switch branch Statements 24: #24. // false: Borrow a very classic interpretation of Damian Wielgosik (new function () {return String ('foo ');}). toString ()! = 'Foo'
25: #25. // "f, o, o": After the string is converted to an array, it is added to the array and converted to a string. You can try adding two arrays by using Damian Wielgosik.

Just consider e.g. [1, 2] + [3, 4] and see how arrays are casted to string.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.