Variable objects and immutable

A variable objects

js seven basic data types: number, string, boolean, null, undefine, object, symbol (ES6 new), in addition to a reference type object, are other basic types

The basic types of stack value is present

The presence of reference type address stack, the stack address points to the value present in the stack

So, if we copy the basic type, a copy of its value, if the assignment alone does not affect the value is copied. If we copy a reference type, it is in fact a copy of the heap address If you change the value of the party, it is to change the value of the heap, because the other point to the same heap, so its value is also synchronized revised .
So for reference types (array, function, obj, etc.) two references in the copy in order to make the type independently of each other, we generally do not directly assign the old array to the new array, but generate a new reference type variable, the old a value in the array are copied to a new array, which is called a deep copy, but if the value of the array is a reference type, then, should this type of reference value and then were taken out and paid the new generation of the reference object, if the nesting level too much, it is easy to deal with mistakes.

Second, immutable objects

 

var obj = Object.preventExtensions ({});
 // directly define new attribute being given 
Object.defineProperty (obj, 'Content' , { 
value: 'Hello' 
}); // TypeError: Can Not DEFINE Property: P, Object . IS not Extensible 
// non-strict mode is not being given added point by symbols, but will fail silently, the original object is still not content property 
object.content = 'the Hello' ; 
object.content: // undefine

 

Object.isExtensible () may determine whether an object can be extended, i.e., whether to add a new attribute. Parameter is the target object, and returns a Boolean value, true representatives of scalable, false can not be extended.

2.Object.seal()

Object.seal () can make an object can not add new attributes and also can not delete the old property, parameter is the target object, the object is returned after the modification.
Its nature is achieved by modifying the object Configurable (may be provided) to false. In the properties mentioned in the description of the object, when configurable is false, other configurations can not be changed, writable only true becomes false, and the property can not be deleted. Writable or whenever one of which is configurable true, value is variable, so the object can be changed after sealing or attribute value.

 1 var obj={content:'hello'};
 2 object.getOwnPropertyDescriptor(obj,'content');
 3 //obj{
 4 //configurable: true
 5 //enumerable: true
 6 //value: "hello"
 7 //writable: true
 8 //}
 9 object.seal(obj);
10 object.getOwnPropertyDescriptor(obj,'content');//seal后configurable变为false
11 //obj{
12 //configurable: false
13 //enumerable: true
14 //value: "hello"
15 //writable: true
16 //}

Corresponding Object.isSealed () check whether an object can be sealed, the parameter is the target object, and returns a Boolean value, true represents sealed, not deletions properties for false can not be sealed deletions properties.

var obj = new new Object (); 
object.isExtensible (obj); // to true 
object.isSealed (obj) // to false 
Object.seal (obj); 
object.isExtensible (obj) // to false, the object seal after isExtensible () also changed 
object.isSealed (obj); // to true

3.Object.freeze ()
Object.freeze () can make an object can not add new attributes, you can not delete the old property, and can not modify the value of the property. Parameter is the target object, the object returned after modification.

var obj = Object.freeze ({name: 'Example' });
 // directly define new attribute being given 
Object.defineProperty (obj, 'Content' , { 
value: 'Hello' 
}) // TypeError: DEFINE CAN Not Property : the p-, IS not Extensible Object. 
// non-strict mode by adding dot notation does not complain, but will fail silently, the original object is still not content property 
obj.content = 'the Hello' ; 
obj.content; // undefined; 
delect obj.name; // delete failed, return false 
obj.name = 'the Hello' ; 
obj.name; // still the 'example'

Corresponding Object.isFrozen () check whether an object can be frozen, i.e., whether additions and deletions. Parameter is the target object, and returns a Boolean value, true again and again said that it has not frozen falsification, false representation unfrozen.

var obj = new new Object () 
object.isExtensible (obj); // to true 
object.isSealed (obj); // to false 
object.isFrozen (obj); // to false 
Object.freeze (obj); 
object.isExtensible (obj) ; // to false, the object isExtensible Freeze () also changes 
object.isSealed (obj); // to true, the object after isSealed Freeze () also changes 
object.isFrozen (obj); // to true

Note that
both can not be extended, seal, or freeze, the control is shallow, i.e. only additions and deletions to the property object itself. If the object attribute is a reference type, such as an array or an object subObj subArr the like, although subArr, subObj can not be falsified, but their properties may be additions and deletions.

var obj = Object.freeze ({ 
Content: {name: 'Example' } 
}); 
obj.content = new new Object (); 
obj.content; // {name: 'Example'}, Content itself can not be modified 
obj.content = .name 'Test' ; 
obj.content; // {name: 'Test'}, but still changes the properties of content, obj is not frozen because of its properties

Since each object has a _proto_ value of the property is the prototype of the object to, is a reference type, due to freezing is shallow, so the prototype object will not be attached to freeze, you can still give the prototype by object add new properties to achieve the object attribute to the effect of the current object. So if you want further freeze also you need to be frozen on the prototype object.

var obj = Object.freeze ({}); 
obj.content = 'Hello' ; 
obj.content; // undefine, increased failure 

var proto = Object.getPrototypeOf (obj); 
proto.content = 'Hello' // obj of round attribute Hello 
obj.content; // obj content does not have this property, then this property to find its prototype, 'hello' to increase the success 

Object.freeze (proto); 
proto.name = "Example" ; 
obj.name // undefine, failure to increase after freeze prototype

Object.defineProperty ()
Object.defineProperty () method defines an object directly on a new attribute, or modify an existing attribute object, and return the object.

语法    object.definePoperty(obj,prop,descriptor)

Parameters
obj to define attributes in the object on which the
name of the attribute to be defined or modified prop
will be defined or modified descriptor attribute descriptor

Return Value
object is passed to the function
in ES6, since the particularity of the type Symbol, Symbol values do with the object type of conventional key defining or modifying different, object.defineProperty is defined as the key attributes of a method Symbol one.

Description
This method allows to add or modify the properties of an object. Ordinary operations by assigning attributes to add are enumerable, can be presented in between attributes enumerated (for ... in or object.keys method), the value of these attributes can be changed, it can be deleted. This method allows additional options to modify the default (or configuration). By default, use Object.defineProperty () to add the attribute value can not be modified.

Attribute descriptor
objects in the existing attribute descriptor two primary forms: the data access descriptor and the descriptor. Data descriptor is an attribute having a value, the value may be written to, may not be written. Access descriptor is a function of the getter-setter properties described herein. Descriptor must be one of two forms; not both.

Access data descriptor and a descriptor has the following optional key **:
configurable
if and only if the configurable attribute is true, the attribute descriptor to be able to be changed, but also the properties from the corresponding object been deleted. The default is false.
enumerable
if and only if the enumerable attribute is true, the property to be able to appear in the enumeration property of the object. The default is false.
Data descriptor also has the following optional key:
value
of the corresponding property value. JavaScript may be effective values (numerical, objects, functions) the default undefine
writable
if and only if the writable attribute is true, value assignment operator can be changed, the default is false
access descriptor also has the following optional key:
get (defaults to undefined)
a property provides getter methods, if there is no getter was undefined. When accessing the property, which will be executed when the method is no argument to perform, but will pass this subject (due to the inheritance, this here is not necessarily define the object of the property).
set (default is undefined)
a property setter methods provided, if no setter was undefined. When the attribute value modification is triggered to perform the method. The method receives unique parameters, i.e., the properties of the new parameter value

Guess you like

Origin www.cnblogs.com/xiangW/p/10993803.html