JavaScript Object-oriented essentials--Understanding objects
Although there are a number of built-in reference types in JavaScript, it is likely that you will frequently create your own
Object. The objects in JavaScript are dynamic.
I. Defining attributes
When a property is added to an object for the 1th time, JavaScript calls a
[[Put]], the method creates a new node on the object that holds the property, just like
The first time a key is added on a hash table This operation not only specifies the initial value
Some characteristics of attributes are also defined
1.1 [[Put]] Internal method
[[Put]] Create an own property on the object
1.2 [[Set]] Internal method
When a property is given a new value, the [[Set]] internal method on the calling object
var person1 = { ' Nicholas '// call [[Put]}var on Person1 New// call [[Put]] on Person2// call Person1 on [[Set]]// invoke [[Set]] on Person2
Two. Attribute detection
Since attributes can be added at any time, it is sometimes necessary
Checks whether an object has properties on it
If the value in the If judgment is an object, non-empty string, not 0 digits, and false
The value is: null, underfined, 0, False, Nan, or
The empty string is false.
The
2.1 in probe
in operator checks its own properties and prototype properties.
var person = {name: ' Nicholas ' }console.log ( ' name ' in person) // true; Console.log (' Age ' in person) // false; Console.log (' toString ' in person) // Span style= "color: #008000;" >true; 2.2 hasOwnProperty () probing obj.hasownproperty ( ' name ' ' name ')) // true; Console.log (Person.hasownproperty (' toString ')); // false
Three. Delete Attributes
When the delete operator deletes an attribute on an object, it calls [[Delete]] on the object.
Internal method, you can assume that the operation removes a key-value pair from the hash table.
The delete operation returns true successfully.
var person = { ' Nicholas '}console.log (in//true; Delete // call [[Delete]] to return true inch // false // undefined
Four. Attribute Enumeration
4.1 For-in Enumeration
All added properties are enumerable by default and can be for-in loop-friendly
They, the enumerable attribute internal features [[[Enumerable]] are set to True
For-in enumerates all the enumerable properties of an object and assigns the property name
to a variable
var Property ; for inch object) {Console.log (' Name ' + property); Console.log (' Value ' +Object[property]);}
4.2 Object.keys () method
Object.keys (obj) can get an array of names of enumerable properties
Returns only its own property and does not traverse prototype properties
var properties = Object.keys (obj);
var I,len
for (i = 0,len = properties.length;i<len;i++) {
Console.log (' name ' + properties[i]);//Key Name of the object
Console.log (' value ' = obj[properties[i]);//The value of the object
}
4.3 propertyisenumerable () method
Not all objects are enumerable, in fact, the
[[Enumerable]] feature of most native methods is true, you can use the The propertyIsEnumerable ()
method checks whether a property is enumerable. Each object has this method.
var person = { ' Nicholas '}console.log (in//true// true; var properties = Object.keys (person), Console.log (in//true; // false;
Five. Attribute types
There are two types of properties: Data properties and accessor properties.
Accessor properties do not contain a value but define a function that is called when the property is read
Getter and a function setter that is called when a property is written.
5.1 Getter and setter
Defines an accessor property name, _name holds the actual value of the accessor property.
(_ Convention naming conventions).
varperson ={_name:' Nicholas ', get Name () {Console.log (' Name '); return This. _name;//Return property value}, set name (val) {Console.log (' Set name Val '); This. _name = val;//Setting property values}}console.log (person.name); //back to ' Nicholas 'Person.name = ' Greg ';//If only the getter is set, the value of name cannot be changedConsole.log (Person.name);//back to ' Greg '
You don't necessarily define setter and getter at the same time, if only getter is defined
This property becomes a read-only
Six. Attribute characteristics
ECMASCRIPT5 introduces several ways to interact with attribute features
6.1 Common features [[Enumerable]] [[configurable]]
[[Enumerable]] Determines whether the property can be traversed;
[[configurable]] Determines whether the property can be configured;
The Object.defineproperty () method can be used to change the characteristics of a property.
Accepts 3 parameters: The property's object, the property name, and the object that sets the feature.
varperson ={name:' Nicholas '}object.defineproperty (person,' Name ', {enumerable:falseConfigurable:false}) Console.log (' Name 'inchPerson//true;Console.log (person.propertyisenumerable (' name '))//false;varPropers =Object.keys (person); Console.log (propers.length)//0DeletePerson.name;//Cannot deleteConsole.log (Person.name)//' Nicholas 'Object.defineproperty (person, ' name ', {configurable:true //Error})
6.2 Data Attribute Characteristics
The Data property contains two characteristics that the accessor property does not have.
[[Value]] and [[writable]]
[[value]] saves the value of the property;
[[writable]] property can be written to
var person = { ' Nicholas '}object.defindeproperty (person,' name ', { ' Greg ', true, true, true})
When Object.defindeproperty () is called, the property is first checked for existence.
If it does not exist, the value is created based on the characteristics specified by the object.
When you use Object.defindeproperty () to create attributes, be sure to remember to
All features have a value, otherwise the feature of the Boolean is set to False by default
6.3 Accessor Properties
Accessor properties also have two additional characteristics, accessor properties do not need to store values
Therefore there is no [[Value]] and [[writable]]. instead [[Get]]
and [[Set]] are the same as the getter and setter in the object face form.
varperson ={_name:' Nicholas ', get Name () {return This. _name; }, set name (val) { This. _name =Val; }}//This code can be changed to: varperson ={_name:' Nicholas '} object.defineproperty (person,' Name ', {get:function(){ return This. _name; }, Set:functiono (val) { This. _name =Val; }, Enumerable:true, configurable:true})
6.4 Defining multiple attributes
Use Object.defineproperties (obj,{}) for an object
Defining characteristics of multiple properties
varperson ={};object.defineproperties (person,{_name: {value:' Nicholas ', Enumerable:true, Configurable:true, writable:true},name: {get:function(){return This. Name;},set:function(val) { This. Name =val;},enumerable:true, Configurable:true}})
Defines the _name Data property and the name accessor property.
6.5 Getting the characteristics of a property
Use the Object.getownpropertydescriptor () method to return a property's characteristics
An object even if there is no definition displayed.
Accepts the object, property name of two parameter properties.
var = = ' Nicholas '}var descriptor = object.getownpropertydescriptor (person , ' name '//true//True//true//' Nicholas '
Seven. Prohibit modification of objects
[[extensible]] is a Boolean value that indicates whether the object feature can be modified.
All objects you create are extensible by default, meaning that new properties can be
Add to
7.1 Prohibit expansion
The Object.preventextensions () method creates an object that is not extensible.
Object.isextensible () to check the value of [[extensible]].
var person = { ' Nicholas '//ture//flase function() {console.log (this. name);}; Console.log (in//false
7.2 Object Seal
The object seal is the second method of creating a non-extensible object.
The object of a seal is not extensible and all properties are not configurable
If an object is sealed, it can only read and write his attributes.
Use the Object.seal () method to seal an object
when invoked [[extensible]] and [[configurable]], the feature is false.
You can use object.issealed () to determine whether an object is sealed.
var person = { ' Nicholas '/// whether to extend trueConsole.log (object.issealed ( person)) // is the seal false// Seal Object // whether extended false Console.log (object.issealed (person)) // is the seal true
7.3 Object freezes
The third way to create a non-extensible method is to freeze it.
If an object is frozen, you cannot add and remove properties on it
You cannot change the property type, nor can you write any data properties.
Object.freeze () to freeze an object
Object.isfrozen () Determines whether an object is frozen.
varperson ={name:"Nicholas"}console.log (object.isextensiable (person))//whether to extend trueConsole.log (object.issealed (person))//whether the seal is falseConsole.log (Object.isfrozen (person))//whether to freeze falseObject.freeze (person)//Freeze ObjectsConsole.log (object.isextensiable (person))//whether to extend falseConsole.log (object.issealed (person))//whether the seal is trueConsole.log (Object.isfrozen (person))//whether to freeze true
03.JavaScript Object-oriented essentials--Understanding objects