preface

The book is connected to the text, these days to some of the more unpopular knowledge points and made some summaries, write down here, to avoid forgetting.

1. Object tamper-proof

Prevents misoperations on the specified object

Var obj= {a:1} obj.__proto__. Fn =function (){return 1} object.preventExtensions (obj) obj.name = 'test'; The console. The log output (obj) # {a: 1, __proto__ : {fn: ƒ (); # close tamper-proof var obj}} = {a: 1} obj. __proto__, fn = function () {return 1} / / Object. PreventExtensions (obj) obj. Name = 'test'; The console. The log output (obj) # {a: 1, name: "test" __proto__ : {fn: ƒ (); }}Copy the code

2. Check whether the object is extensible

The object.isextensible () method can be used to determine whether an Object isExtensible, whether it is defined by object.preventextensions, object.seal, or object.freeze

Object. IsExtensible (obj) 1. var obj= {a:1} object.preventExtensions (obj) Console. log(Object.isextensible (obj)) // Output false 2. var obj= {a:1} object.seal (obj); Console. log(object.isextensible (obj)) // Prints falseCopy the code

3. NaN is of type number

var a = 1 * 'a'; Console. log(a) console.log(typeof a) // Outputs NaN numbersCopy the code

4. Quickly flatten the array

Var a = [1, 2, 3, 4]]. a.flat(); console.log(a); // output [1,2,3,4] flat function default flat 1 example: [1, 2, 3, [45]]]. Flat () / / output [1, 2, 3, [45]] can use digital leveling specified level, Or use Infinity to flatten any level [1,2,[3,[45]]. Flat (Infinity)Copy the code

5. Json.stringify encounters undefined, function and symbol

JSON. Stringify (undefined) // output undefined JSON. Stringify (function(){}) // output undefined JSON Undefined JSON. Stringify ([1, 2, undefined, 1)) / / output "[1, 2, null, 1]" JSON. The stringify ({a: 1, b: undefined}) / / output "{" a" : 1}"Copy the code

6. Quickly obtain the timestamp of the current time

+ new Date() performs an implicit type conversion, calling the valueOf method of the object, equivalent to the following method: new Date().valueof ()Copy the code

7. ParseInt differs from Number

var a = 123+'abc'; Console. log(Number(a)) console.log(parseInt(a)) Number NaN parseInt will be stopped if it is not a NumberCopy the code

9. Turn a pseudo-array into an array

Array. The prototype. Slice. The call () example: function test () {let args = Array. The prototype. Slice. The call (the arguments); console.log(args); } test(1,2,3)Copy the code

10. ParseInt The second argument

Var arr = [' 1 ', '2', '3'). The map (parseInt) console. The log (arr) / / [1, NaN, NaN] resolution: Var arr = ['1', '2', '3']. Map ((item,index)=>{parseInt(item,index)}) 1. ParseInt (3,2) is converted to base 2. If 3 is greater than 2, NaN is returned with the first argument as a numeric value and the second as a base numberCopy the code