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