Effective JavaScript: Fourth Chapter

Source: Internet
Author: User
Tags closure

1. Understanding: Differences between prototype, getprototypeof and _proto_

①c.prototype is used to build prototypes of objects created by new C ().

②object.getprototypeof (obj) is the standard method used in ES5 to get the prototype object of the Obj object.

③obj._proto_ is a non-standard way to get the prototype object of an Obj object.

A class is essentially a combination of a constructor and a prototype object that is used to share a method between instances of that class.

2. using closures to store private data

JavaScript objects All property names are a string, any program can simply access the property name to obtain the corresponding object properties. For example, features such as the for......in Loop, the ES5 Object.keys (), and the Object.getownpropertynames () function can easily get all the property names of an object.

Closures are a simple data structure that stores data in closed variables without providing direct access to those variables. The only way to get the inner structure of a closure is for the function to show the way to get it.

The closure variable is private and can only be obtained by a local reference.

Local variables are used as private data to conceal information through methods.

3.implicit binding of this variable

Each function has an implicit binding of this variable. The binding value of the This variable is determined when the function is called. The scope of the this variable is always determined by its nearest enclosing function. Using a local variable makes the this binding available for intrinsic functions.

4. avoid inheriting standard classes

Subclasses of inherited standard classes tend to be corrupted due to some special intrinsic properties (such as [[Class]]). For example:

function Dir (path,entries) {    this. Path = path;       for (var i = 0, n = entries.length; i < n; i + +)        {this [i] = entries[i];    = Object.create (Array.prototype);    

This method destroys the expected behavior of the length property. The Length property works only for special objects that are internally marked as "real" arrays. The ECMAScript standard specifies that it is an invisible intrinsic property called [[Class]]. When we extend the Array class, an instance of the subclass is not created by the new array () or literal [] syntax, so the [[class]] property value of the instance of Dir is "Object". So we can define an instance property of an entries array:

function Dir (path, entries) {    this. Path = path;      this. entriess = entries;}

The method of redefining array in the prototype, we can delegate these corresponding methods to the entries property to complete.

function (f, thisarg) {    if(typeof Thisarg = = = "undefined"        ) {this ;    }  This . Entries.foreach (f, Thisarg);}

Most constructors in the ECMAScript standard library have similar problems. Some properties or methods expect to have the correct [[class]] property or other special intrinsic properties, but the subclass is not available. For this reason, it is best to avoid inheriting the following standard classes: Array, Boolean, Date, Function, number, regexp, or string.

Effective JavaScript: Fourth Chapter

Related Article

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.