Is JAVASCRIPT simple?

Source: Internet
Author: User

1. Have you tried to sort a set of numbers?

The Javascript sort () function is sorted by default using alphanumeric (string Unicode code points).

So [1,2,5,10].sort () outputs [1, 10, 2, 5].

To properly sort an array, you can use [1,2,5,10].sort ((A, b) + = a?—? b)

It's a simple solution, if you have to know there's a hole.

2. New Date () very good

New Date () can accept:

    • No parameters: Returns the current time

    • A parameter x: Returns January 1, 1970 + x milliseconds. People who understand Unix know why.

    • New Date (1, 1, 1) returns 1901, February, number 1th. Because the first parameter represents 1900 plus 1, the second parameter represents the second month of the year (and therefore February)?—? the normal brain loop will be indexed from 1?—?, the third parameter is obviously the first day of the month, so 1?—? Sometimes the index does start at 1?—?。

    • New Date (2016, 1, 1) does not add 2016 to 1900. It stands for 2016 only.

3. Replace does not "replace"

Let S = "Bob"= s.replace (' B ', ' l '= = = ="lob" = = = "Bob")

Think it's a good thing, because I don't like functions that change their input. You should also know that replace replaces only the first matching string:

If you want to replace all matching strings, you can use a regular expression with the/g flag:

// Replace all matching strings

4. When comparing, pay attention to

// these is ok // true1 = = = 1         //  true//  Theseis not[//  False1} = = = {A:1}   //  false= = = {}           //  False

Reason: [Three-to-one] and [two] are independent arrays. They just happen to contain the same values. They have different references and cannot be compared with = = =.

5. The array is not the original data type

typeof {} = = = ' object '  //  truetypeof//  truetypeof 1 = = number     //  true// but ... . typeof [] = = = ' object '  //  true

If you want to know if your variable is not an array, you can still use Array.isarray (MyVar)

6. Closures

This is a very famous interview question:

Const GREETERS = [] for (var i = 0; i < ten; i++) {    Greeters.push (function () {        return  console.log (i)    })}greeters[//    greeters[//  greeters[//  

Do you think it will output 0, 1, 2 ...? Do you know why it's not output like this? How would you modify let it output 0, 1, 2 ...?

Here are two possible workarounds:

Replace Var. Boom with Let. Solved.

The difference between let and Var is scoped. The scope of VAR is the most recent function block, let's scope is the nearest enclosing block, the enclosing block can be smaller than the function block (if not in any block, then both allow and Var are global).

Workaround: Use bind:

Greeters.push (Console.log.bind (null, i))

There are many other ways. It's just my two first choice.

7. When it comes to bind

What do you think this will output?

Class Foo {

Constructor (name) {

THIS.name = Name

}

Greet () {

Console.log (' Hello, this is ', this.name)

}

Somethingasync () {

Return Promise.resolve ()

}

Asyncgreet () {

This.somethingasync ()

. Then (This.greet)

}

}

New Foo (' dog '). Asyncgreet ()

If you think this program will crash prompt cannot read property ' name ' of undefined, give you a point.

Cause: Greet is not running in the correct context. Similarly, there are still many solutions to this problem.

I personally like

Asyncgreet () {

This.somethingasync ()

. Then (This.greet.bind (This))

}

This ensures that an instance of the class calls greet as a context.

If you think that greet should not run outside the instance context, you can bind it in the constructor of the class:

Class Foo {

Constructor (name) {

THIS.name = Name

This.greet = This.greet.bind (This)

}

}

You should also know that the arrow function (= =) can be used to preserve the context. This method can also:

Asyncgreet () {

This.somethingasync ()

. then (() = > {

This.greet ()

})

}

(reproduced in this article)

Is JAVASCRIPT simple?

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.