Js-javascript Advanced Programming Study Note 19

Source: Internet
Author: User

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

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.