<script>function p(){ var len=arguments.length; for(var i=0;i<len;i++){ document.write(arguments[i]+"<br/>"); } }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>Existing problems
1. Since all instances copy entities defined by the same method, efficiency (memory efficiency and inefficient execution) can be solved through prototype inheritance.
2. Unable to access control of attribute values (private, Public) can be solved through closures
The operation object accessed by attributes is not a variable but a reference to the object
Processing of reading only integer parts of numeric values
Math[this<0?'celling':'floor'](this);
Associative array
In js, you must pass objects to implement associative arrays
Basic operations are made by keys, setting elements, and deletion of elements.
<script>var map={x:3,y:4};p(map.x);delete map.x; //truep(map.x); //undefined The result of accessing non-existent elements is undefined. Since the value can be set to undefined, it is impossible to judge whether the value exists by comparing the value with undefined. You can enumerate it by for in a='undefined';p(a);//undefinedp(typeof map.x==a); //true</script>As an associative array, you should pay attention to
<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]); //The attributes inherited through the prototype will be enumerated}//x:1 y:2 z:5delete obj.x;//truep(obj.x); //undefinedp(obj.z); //5//The attributes inherited through the prototype cannot be deleted delete obj.z; //truep(obj.z);//5//When using objects as an associative array, literals are usually created. Even if the view uses an empty object literal to create an associative array without elements, the prototype's attribute p('toString' in obj); //truevar obj1={};p('toString' in obj1);//true//Enumenp(obj1.length); //undefinedfor(var k in obj1){ p(obj1[k]);}//No elements are enumerated. This is because of the enumerable attribute.//Truevar obj1={};p('toString' in obj1);//true//Enum enumeration of p(obj1.length); //undefinedfor(var k in obj1){ p(obj1[k]);}//No elements are enumerated. This is because of the enumerable attribute.//Truevar obj1={};p('toString' in obj1);//True//Self-like attribute var inherited by participating in the prototype map={};p(map.hasOwnProperty('toString')); //falsemap['toString']=1;p(map.hasOwnProperty('toString')); //truedelete map['toString'] ;p(map.hasOwnProperty('toString')); //false</script>Attributes
The attributes of the object also have some attributes
The following table summarizes the attributes defined in the fifth edition of ECMAScript, and the attribute value is defined as value attributes.
Table 1
Attribute name | meaning |
writable | The value of the attribute can be rewrite |
enumerable | Can be enumerated by for in |
configurable | You can change the attributes and delete the attributes |
get | Getter function that specifies property values |
set | Setter function that can specify attribute values |
Immutable Objects
That is, an object whose state cannot be changed after generation, and a string object is a typical immutable object.
Flexible use of immutable objects can improve the robustness of the program, such as when passing it to method parameters, there is a method to rewrite the object content, etc.
Immutable objects can be implemented in js
1. Hide the attribute (status hidden) and do not provide change operations (closure implementation)
2. Flexible use of functions provided by ECMAScript in the fifth edition
3. Flexible use of writable, configurable attributes, setters and getters
Functions used to support immutable objects in ECMAScript version 5 are shown in the following table
Method name | New attributes | Attribute Delete | Change of attribute value | 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);//Cannot add the attribute obj.z=4;p(Object.keys(obj));//x,y//Cannot delete the attribute obj.y;p(Object.keys(obj)); //x//Cannot add the attribute value obj.x=20;p(obj.x); //20//Object.seal example sets the configurable of the attribute to false var obj={x:2,y:3};Object.seal(obj);//Cannot add or delete obj.z=3;p(Object.keys(obj)); //x,ydelete obj.x; //falsep(Object.keys(obj));//x,y//can change the attribute value obj.x=20;p(obj.x);//20//Object.freeze Example sets the writable of the attribute to false var obj={x:2,y:3};Object.freeze(obj);//cannot add or delete, nor can the attribute value obj.z=3;p(Object.keys(obj)); //x,y//can change the attribute value obj.x=20;p(obj.x);//20</script>Need to pay attention
1. For the above three methods, it cannot be restored once they are changed.
2. If you want to make the inherited method in prototype inheritance unchanged, you need to display it.