This article mainly introduces and summarizes the js constructor, the implementation method and use of the associated array, and the immutable object and its implementation methods and the points worth attention during their use, for more information, see
《script》function p(){ var len=arguments.length; for(var i=0;i
"); } }function Myclass(x,y){ this.x=x; this.y=y; this.show=function(){ return this.x+this.y; }}var m1=new Myclass(1,2);var m2=new Myclass(3,4);p(m1.show(),m2.show());《script》
Problems
1. Because all instances copy the objects defined by the same method, the efficiency (low memory efficiency and execution efficiency) can be solved through prototype inheritance.
2. Access Control (private and Public) for attribute values cannot be implemented through closures.
Attribute access calculation object is not a variable but an object reference
Processing of reading only the integer part of a value
Math [this <0? 'Celling': 'floor '] (this );
Join Array
In js, you must use an object to associate an array.
Basic operations include key value, element setting, and element deletion.
Script var map = {x: 3, y: 4}; p (map. x); delete map. x; // truep (map. x); // The undefined access result for non-existent elements is undefined. Because the value can be explicitly set to undefined, you cannot compare the value with undefined to determine whether the value exists, you can use for in to enumerate a = 'undefined'; p (a); // undefinedp (typeof map. x = a); // true script
Notes for associating Arrays
Script function Myclass (x, y) {this. x = x; this. y = y;} Myclass. prototype. z = 5; var obj = new Myclass (1, 2); for (var key in obj) {p (key + ":" + obj [key]); // enumerate attributes inherited by prototype} // x: 1 y: 2 z: 5 delete obj. x; // truep (obj. x); // undefinedp (obj. z); // 5 // attributes inherited by the prototype cannot be deleted by delete. delete obj. z; // truep (obj. z); // 5 // when using an object as an associated array, it is usually created using a literal, even if the view uses an empty Object literal to create an associated array without elements, it still inherits the prototype property p ('tostring' in obj) from the Object class ); // truevar obj1 ={}; p ('tostring' in obj1); // true // enumerate p (obj1.length) through for in; // undefinedfor (var k in obj1) {p (obj1 [k]);} // No elements are enumerated. This is because of the enumerable attribute. // use hasOwnProperty to determine whether it is its own attribute or the property var map inherited by the prototype {}; p (map. hasOwnProperty ('tostring'); // falsemap ['tostring'] = 1; p (map. hasOwnProperty ('tostring'); // truedelete map ['tostring']; p (map. hasOwnProperty ('tostring'); // false script
Attribute attributes
Some attributes of the object are also
The following table summarizes the attributes defined in the fifth edition of ECMAScript. The attribute value is set to a value attribute.
Table 1
Attribute name |
Description |
Writable |
The attribute value can be rewritten. |
Enumerable |
You can use for in to enumerate |
Retriable |
You can change or delete attributes. |
Get |
The getter function that can specify the attribute value |
Set |
Setter function that can specify attribute values |
Immutable object
That is, an object whose status cannot be changed after generation. A String object is a typical immutable object.
Flexible Use of immutable objects can improve program robustness. For example, when a method is passed to a method parameter, there are methods that rewrite the object content.
The following methods can be used to implement immutable objects in js:
1. Hide attributes (State hidden) and do not provide change operations (Closure implementation)
2. flexible use of functions provided by the fifth edition of ECMAScript
3. flexible use of writable, retriable attributes, and setter and getter
For functions used in the fifth edition of ECMAScript to support object immutations, see the following table.
Method Name |
Attribute added |
Attribute Deletion |
Attribute Value Change |
Confirmation Method |
PreventExtensions |
X |
O |
O |
Object. isExtensible |
Seal |
X |
X |
O |
Object. isSealed |
Freeze |
X |
X |
X |
Object. isFrozen |
Object. preventExtensions example
Script var obj = {x: 2, y: 3}; Object. preventExtensions (obj); // The property obj cannot be added. z = 4; p (Object. keys (obj); // x, y // you can delete the delete obj attribute. y; p (Object. keys (obj); // x // you can change the property value obj. x = 20; p (obj. x); // 20 // Object. in the seal example, set the retriable property to false var obj = {x: 2, y: 3}; Object. seal (obj); // The object cannot be added or deleted. z = 3; p (Object. keys (obj); // x, ydelete obj. x; // falsep (Object. keys (obj); // x, y // you can change the property value obj. x = 20; p (obj. x); // 20 // Object. in the freeze example, set the writable property to false var obj = {x: 2, y: 3}; Object. freeze (obj); // neither add nor delete nor change the attribute value obj. z = 3; p (Object. keys (obj); // x, ydelete obj. x; p (Object. keys (obj); // x, y // you can change the property value obj. x = 20; p (obj. x); // 20 script
Note:
1. Once the above three methods are changed, they cannot be restored.
2. If you want to make the inherited methods in prototype inheritance unchangeable, You need to display them.