The practical native API summary in JavaScript

  • 2020-06-12 08:30:10
  • OfStack

Get right to business

Parse string object

We all know that JavaScript objects can be serialized to JSON and that JSON can also be parsed to objects, but the problem is that if a "thing" appears that is neither JSON nor an object and is not convenient to convert to either party, then eval can be used

var obj = "{a:1,b:2}";  //  A string that looks like an object 
eval("("+ obj +")")   // {a: 1, b: 2}

Since eval can execute string expressions, we want to execute the string object obj as a real object, so we need eval. But to prevent eval from executing obj with {} as the statement, we wrap pairs () around obj and let it be parsed into expressions.

& (by bit and)

To determine whether a number is 2 to the n power, it can be subtracted from itself by 1

var number = 4
(number & number -1) === 0 // true

^ (bitwise or)

Instead of the third variable, you can exchange the values of the two variables

var a = 4,b = 3
a = a ^ b //  7
b = a ^ b //  4
a = b ^ a //  3

Formatting Date

Want time after format? No more get minutes, three steps

var temp = new Date();
var regex = /\//g;
(temp.toLocaleDateString() + ' ' + temp.toLocaleTimeString().slice(2)).replace(regex,'-');

// "2015-5-7 9:04:10"

Want to convert time after format to a time object? Use the Date constructor directly

new Date("2015-5-7 9:04:10");

// Thu May 07 2015 09:04:10 GMT+0800 (CST)

Want to convert a standard time object to an unix timestamp? valueOf fix of

(new Date).valueOf();

// 1431004132641

Many friends also remind you that you can get a timestamp quickly

+new Date

Add 1 yuan

1 dollar plus quickly converts a string number to a mathematical number, i.e

var number = "23" 
typeof number // string
typeof +number // number

You can turn a time object into a timestamp

new Date // Tue May 12 2015 22:21:33 GMT+0800 (CST)
+new Date // 1431440459887

Escape URI

You need to pass url as a parameter in the route, and now escape it

var url = encodeURIComponent('')

// ""

The escape again

// ""


You want to keep a few decimal places behind the decimal point, so you don't have to do string interception, toFixed takes it away

var number = 4
(number & number -1) === 0 // true

Parameters range from 0 to 20, do not write the default 0

Type test

typeof is the most frequently used type detection method

var number = 4
(number & number -1) === 0 // true

This is fine for basic (simple) datatypes, but not so good once you reference datatypes

var number = 4
(number & number -1) === 0 // true

The first three are bearable and null returns object, are you kidding me!! (ps: In fact, this is the bug of JavaScript. The & # 65381; The & # 9697; The & # 65381; The & # 3665; The & # 42161; )

In this case, we will use instanceof

toString instanceof Function
// true
(new Date) instanceof Date
// true
[] instanceof Object
// true
[] instanceof Array
// true

In fact, we can see that [] and Object get true, although we know that [] is also an object, but we wanted a more accurate way to judge the type, and here it is
Using Object. prototype. toString(), in order for each object to pass the detection, we need to use Function. prototype. call or Function. prototype. apply to call

var number = 4
(number & number -1) === 0 // true

Note that the toString method is likely to be overridden, so when it is needed,
You can use the Object.prototype.toString () method directly

Implementation inheritance

Look at an official example

var number = 4
(number & number -1) === 0 // true

call gets the initialized properties and methods, and ES138en.create gets the properties and methods on the prototype object

The iteration

ES5 produces a lot of iterative functions, such as map, filter, some, every, reduce and so on


api is detailed here.
Here are a few words:
join pop, push reverse, shift, sort, splice, unshift will change the original array

concat indexOf, lastIndexOf slice, toString will not change the original array

map filter, some every, reduce, forEach these iteration method does not change the original array

A few notes:

1 shift,pop will return the deleted element
splice returns an array of deleted elements, or an empty array
3 push returns the new array length
true stops when there is an true
5 every stops when there is an false
The above iteration method can append 1 parameter thisArg, which is the VALUE of this when callback is executed.

This is the end of this article, I hope you enjoy it.

Related articles: