1. Have you tried to sort the array elements?
JavaScript is sorted by default using the dictionary order (alphanumeric). Therefore, [1,2,5,10].sort () results are [1, 2, 5] .
If you want the right sort, you should do this: [1,2,5,10].sort ((A, B) +-a)
2. New Date () very useful
new Date()
Methods of Use are:
- Do not receive any parameters: returns the current time;
- Receive a parameter
x
: Returns x
the value of January 1, 1970 + milliseconds.
new Date(1, 1, 1)
Return February 1, 1901.
- However, it
new Date(2016, 1, 1)
will not add 2016 on the basis of 1900, but only in 2016.
3. The replacement function is not really replaced?
Let S = "Bob"= s.replace (' B ', ' l '// will only replace the first B// and the value of S will not change
If you want to replace all B, use the regular:
"Bob". Replace (/b/g, ' l ') = = = ' LOL '
4. Careful comparison operations
// these can be // true // true // but these are not . // false // false // false
Because [three-way] and [two] are different arrays, only their elements happen to be the same. Therefore, it cannot be judged simply by passing it on ===
.
5. Array is not the underlying type
typeof // true typeof // true typeof // true // but ..... typeof // true
If you want to determine var
whether a variable is an array, you need to use Array.isarray (var) .
6. Closures
This is a classic JavaScript interview question:
Const GREETERS = [] for (var i = 0; i < ten; i++) { Greeters.push (function< /c7>return console.log (i)})}greeters[// /// // Ten
Although the expected output 0,1,2,..., it actually does not. Know how to debug?
There are two ways of doing this:
- Use
let
rather than var
. Note: can refer to Fundebug's other blog ES6 "Let" can replace "var"?
- Use the
bind
function. Note: You can refer to Fundebug's other blog JavaScript beginners must see "this"Greeters.push (Console.log.bind (null, i))
Of course, there are many solutions. These two are my favorite!
7. About
bind
What results does the following code output?
class Foo { constructor (name) { this. Name= name } greet () { Console.log ( this. Name) } Somethingasync () { return promise.resolve () } Asyncgreet () { this. Somethingasync (). So (this. greet) }} New Foo (' dog '). Asyncgreet ()
If you say the program will crash and error: cannot read property ' name ' of undefined.
1, because the 16th line is geet
not executed in the correct environment. Of course, there are many ways to solve this bug!
I like bind
to use functions to solve problems:
// code from http://caibaojian.com/8-javascript-attention.html Asyncgreet () {this.somethingasync ().and (this. Greet.bind ))}
This ensures that the greet
instance of Foo is called, not the local function this
.
2. If you want to greet
never bind to the wrong scope, you can use it in the constructor bind
to bind.
class Foo { constructor (name) { this "this") Greet.bind (this ) }}
3. You can also use the arrow function (= =) to prevent the scope from being modified. Note: You can refer to Fundebug's other blog JavaScript beginners must See "arrow function."
Asyncgreet () { this. Somethingasync () and then (() = >{ this. Greet () })}
8. Math.min () greater than Math.max ()
// false
Because Math.min () returns Infinity, Math.max () returns-infinity.
9.5 Common methods for arrays
.forEach()
method allows you to iterate through each element in the array, allowing you to manipulate each element in the callback function. .forEach()
method does not return a value, you do not need to write in the callback function return
, which is meaningless.
var animals = [' dog ', ' cat ', ' Mouse '];animals.foreach (function(item) { Console.log (item);});
.map()
The method is able to traverse the entire array and then return a new array in which the elements in the new array have been processed by the specified callback function.
If you want to modify each element in the array and then save the modified array to a new array, it is most convenient to use the. Map () method.
var numbers = [2, 4, 6, 8]; var doublenums = Numbers.map (function(element) { return element * 2;}); Console.log (' doublenums: ', doublenums)
.filter()
Method can filter out some elements in the array, you can set the conditions in the callback function, the non-conforming elements will be excluded.
var scores = [3, 5, 7]; var topscores = Scores.filter (function(item) { if (item >) { returntrue; Else { returnfalse; }}); Console.log (' topscores: ', topscores);
indexOf()
Can tell you where an element is in the array, it returns the index value, and if there are duplicate elements in the array, it returns the position of the first element.
var a = [2, 9, 9, +]; var i = A.indexof (9); Console.log (' I: ', i); /* if (a.indexof (7) = = = 1) { //the array does not have this element}* /
.every()
The function of the method is to check each element in the array with the specified callback function, which returns true if the callback function returns true for each element .every()
. Otherwise, .every()
returns false.
If you want to know if all the elements in the array meet a certain condition, using. Every () is the most convenient.
var ages = [%, +, +]; var olderThan18 = ages.every (Element) { return element > }); Console.log (' olderThan18: ', olderThan18);
Some of the questions in JavaScript