JavaScript Professional level eight test answer finishing

Source: Internet
Author: User
Tags pow

The recent Amazon book has been reduced to buy a book of this JavaScript code snippet, and found that the original eight-level test was the initiator of this ... There is no answer to the result.

Then I think it is good to tidy up again, plus some of my own notes, and some extensions, write a summary issued to record.

1,

    1. ["1", "2", "3"]. Map(parseint)

This thing is nothing more than a function parameter problem, remember to live on the answer, really no egg use.

The main parseint is the second parameter, is the base of the analytic number, that is, the input, the range is 2~36.

For example, the second running result of this problem is parseint (' 2 ', 1); The parameter does not correspond to Nan.

The second running result parseint (' 3 ', 2); There are no 3 in the binary, so it is Nan.

2

    1. [typeof null, null instanceof Object]

Null,array's typeof are all object.

But the instanceof is still very rigorous.

However, the following should be noted:

1. Instanceof is an example of whether the former is the latter, so

2. Determine the type of a variable the above two are not good to use this:

Object.prototype.toString.call (XXX). Slice (8,-1);

3

    1. [ [3,2,1]. Reduce(Math. Pow), []. Reduce(Math. Pow)] []

This is the new array API familiar,

Reduce also supports two parameters, Arr.reduce (callback[, InitialValue]) the second parameter ... Just ask him to iterate. Arr and initialvalue two parameters cannot be null at the same time, the previous question is also empty.

There are some minor problems with the answer in the link.

4 is the operator priority problem, do not say.

5

This problem involves the scope and the variable declaration.

JavaScript is not a block-scoped scope, only a function scope. So

if (true) {var hehe = ' hehe '} This is not an error to write.

Because the scope is the same, and of course, the most critical point is that JS in advance declaration.

The code is equivalent to:

  1. var name = ' world! ' ;
  2. (function () {
  3. var name;
  4. If (typeof name = = = ' undefined ') {
  5. Name = ' Jack ';
  6. Console. Log(' Goodbye ' + name);
  7. } Else {
  8. Console. Log(' Hello ' + name);
  9. }
  10. })();

So name is undefined;

The maximum number of 6 JS is 2^53.

7 Pass

8

JS does not have an exact value, because the decimal is caused by the loss of the computer in the binary. The Damned JS does not deal with this error.

9,10

switch is enumerated with = = =, remember.

In addition, the last time the classmate mentioned in the community group:

= = = does not trigger tostring.

One pass

12 with 1

Pass

14

  1. var a = [0];
  2. If ([0]) {
  3. Console. Log(a = = true);
  4. } Else {
  5. Console. Log("wut");
  6. }

If's judgment

= = And if the judgment is not the same

For example if (2) is true, but 2 = = True is False, because at = =, is to convert the Boolean value to a numeric type in the comparison, this time true is 1, so when = =, only 1==true is ture.

Similarly, in the IF, only 0 of the numeric type is false, because the remainder of the Boolean type is true.

Reference object is True

Small Tips

1: Conditional statements can be replaced with && for example a&& B is if (a) && b

2: Other languages like to put 1 as false, put in JS, you can use if (~flag) to make-1 return false.

~ is a bitwise negation, in accordance with the negation and then minus one operation,

For example, you have to judge IndexOf

Then if (~xxx.indexof (' x ')) is much prettier than if (Xxx.indexof (' x ') >-1).

15

    1. []==[]

whether = = or = = = is judged by the index value.

= Given is also the index of the object, so do not a=b=[] such a sub-assignment, it will be a nightmare.

16

    1. ' 5 ' + 3
    2. ' 5 ' - 3

53 and 2, because when a minus sign is encountered, it is first converted to a numeric type.

So what is 3 + ' 5 '-3? Is 32.

What if we want the string type to be converted to a numeric type?

+str or str*1.

Like above Str in the middle, try to use the * * bar,

17

I don't get it, I don't find it ... Tried a lot of numbers, found a point law ... But still don't understand why ...

Pass

Arguments, the strict mode will not be changed.

Correspondingly, there is also a global variable undefined, which cannot be modified in strict mode.

But you can still use function undefined to modify.

Some closures will pass a undefined parameter, which is the fear of being replicated in the global.

20,21,22,23,

Pass

24

2.toString () error is due to parser putting '. ' Parsing for floating-point '. '. So if you want to write code that conforms to this semantics, you should:

25

    1. (function() {
    2. var x = y = 1;
    3. })();
    4. Console. Log(y);
    5. Console. Log(x);

Y is global.

A little complicated, remember that three-face question?

  1. var a = ten;
  2. function test() {
  3. A = + ;
  4. Alert(a);
  5. Alert(this. a);
  6. var a;
  7. Alert(a);
  8. }
  9. Test();

The answer is 100 10 100

Because

  1. var a = ten;
  2. function test() {
  3. var a;
  4. A = + ;
  5. Alert(a);
  6. Alert(this. a);
  7. var A; Declaration in advance
  8. Alert(a);
  9. }
  10. Test();

A scope issue is also involved here.

Finally, say it in a unified way.

    1. var a = + ;
    2. function Test() {
    3. Alert(a);
    4. var a = ten;
    5. Alert(a);
    6. }
    7. Test();

The same is stated in advance so the first alert is undefined.

  1. var a = + ;
  2. function Test() {
  3. Alert(a);
  4. A = ten; //Remove Var to define global variables
  5. Alert(a);
  6. }
  7. Test();
  8. Alert(a);

This is the simplest in fact, do not be the first two to be masked on the OK.

26

Remember, you can

27 with 15

28,29, pass.

30

    1. function foo () Span class= "PLN" > { }
    2. var= foo.
    3. foo.=  "bar"
    4. [oldname< Span class= "pun" >, foo.

The name of the function.

    1. function foo() {}
    2. Alert(foo. Name)
    3. var foo2 = function () {}
    4. Alert(foo2. Name)

What about the problem?

The answer is foo,undefined.

Because the second one? The anonymous function was copied to Foo2, so there is no name.

Thank you for the help of my classmates.

31 with 1

32 with 30

Pass

34

    1. [,,,]. Join(",")

ARR allows the last one to be a comma.

Remember the simplest code that validates IE browser?

!+[1,]

is because of the low version (9 ago?) Forgot) bug,[1,] in IE, ToString is 1, not 1.

35

Don't use keywords, there will always be no problems.

OK, that's probably all. This is a problem that is rarely involved in this.

Constructors are scoped

So

and

A, B () This type of scope is a.

But Remember that this is bound only at execution time, so

When you pay the A.B to C, this has a point to the global ...

So what if it does?

The outside is the same, when you execute the func (), it will point to the global.

JavaScript Professional level eight test answer finishing

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.