The 22nd chapter Advanced Techniques
1. Advanced functions
1, safe type detection.
Typeof,instanceof is not completely reliable.
Safe type detection: Use the ToString () method of object native .
function IsArray (value) { return Object.prototype.toString.call (value) = = "[Object Array]";}
2. Scoped Security Constructors
The constructor internally uses instanceof to check if this is an instance of the correct type. Prevents the attribute from being added to the Window object.
3. Lazy Loading function
Function: Avoid repeated execution capability detection when multiple calls are made.
Method:① modifies the function and returns the new function after the capability detection. ② creates a self-executing anonymous function when declaring a function.
4. Function binding
bind () method, passing in the object as the this value. A function is returned, both call and apply are direct calls to the function .
Mainly used for event handlers as well as settimeout () and SetInterval (). More overhead is required.
5. Function currying
Basic method: Use a closure to return a function.
2. Tamper-proof objects
Once the object is defined as tamper-proof, it cannot be undone.
1. Non-expandable objects
var person={name: "ABC"};object.preventextensions (person);
You cannot add new properties and methods to a person object after calling object.preventextensions(person), but you can modify and delete existing members. Use Object.isextensible () to determine whether an object can be extended.
2. Sealed objects
Sealed objects are not extensible and cannot be deleted, but can be modified.
Object.seal ()
Object.issealed ()
3. Frozen objects
Cannot be extended, sealed, only written if the [[Set]] function is defined.
Object.freeze ()
Object.isfrozen ()
3. Advanced Timer
The way a timer works with a queue is when a specific time passes and the code is inserted, but it is not accidentally executed immediately, but rather it is executed as soon as possible.
1. Repetitive timers
to prevent SetInterval from skipping code, you can use a chain structure that settimeout nested settimeout .
Js-javascript Advanced Programming Study Note 19