Object literal syntax
var person={ name: ' Xiao Wang ', age:18, _pri:233}
1. Objects created using the object literal method cannot be invoked in a way such as Var wang=new person () to access the methods in the object, directly using the form of person.xxx
2. Do not use commas at the end of the last member or throw an error in some browsers
Object member Configuration
Object declaration, some hidden properties are generated by default for each member (property or method) inside, which can be read and configurable:
Object.getownpropertydescriptor () or getownpropertydescriptor ()-Read the hidden properties of a member
Object.definepropertype or object.defineproperties----set the hidden properties of a member
The corresponding hidden information is as follows:
Configurable-whether a member can be deleted, the default is true, and it is important to note that if the property is defined as false, subsequent definitions of true will cause an error.
Object.defineproperty (person, ' name ', { configurable:false }) Object.defineproperty (person, ' name ', { configurable:true })
writable-If the member is writable, the default is True
Object.defineproperty (person, ' name ', { writable:false }) person.name= ' Xiao Li ';//attribute not writable, error in strict mode Console.log (Person.name); Output Xiao Wang, stating that the above sentence is invalid
Enumerable-whether a member can be enumerated, by default, is true, which is primarily used to guard against Object.keys () and for in, which means that the property is set for Object.getownpropertynames () method is not valid.
Get and set-function called when read and write members, default is undefined
At the very beginning of the object definition, we created a _PRI member that indicates that the member should be read internally, that the underscore is just a marker, and that the member cannot be restricted from being accessible within the object. Next we encapsulate a property reader to read the _PRI member, read and write the name arbitrary, this is called pri just for readability
Object.defineproperty (person, ' pri ', { get:function () { ////Do some other operation Console.log (' Prepare to get _pri value ') return _pri; }, set:function (newvalue) { _pri =newvalue } }) person.pri= ' 456 '; Console.log (PERSON.PRI);
If only get indicates that the property value is read-only, only set indicates write-only. The most common scenario for a property reader is to do some of the work that comes with the property before reading or writing it to achieve better encapsulation advanced features
1. I want to inherit an object literal object, what should I do?
Object.create can produce a new object with an inherited attribute, but it is important to note that the reference member of the parent class is shared with the subclass, and when the child class modifies the reference member, the member of the parent class also changes synchronously
var person={ name: ' Xiao Wang ', age:18, _pri:233, gf:[' beans son ', ' Zhang G ', ' TFb '] } var child= Object.create (person); Child.gf.splice (0,1); I broke up with Bean. Console.log (Person.gf.length)//Parent GF also became 2, father and son share girlfriend, is too messy
See, so use this kind of inheritance method must be careful, can not be used best not
2. What if I don't want people to add members to my object?
Object.preventextensions (), which is used to prevent adding members to an object, using Object.isextensible () to determine whether an object can add members
Object.preventextensions (person); Add member invalid, non-strict mode nothing will happen, strict mode will be error person.bankaccount= ' Agricultural Bank of China ' //can still delete members, proved that the Preventextensions method can only block the addition of methods Delete person.age; Console.log (person.age)//undefined, indicating that the deletion was successful
3. I don't want anyone to add or remove members
Object.seal () is used to prevent the addition or deletion of members and to determine whether the object is sealed and can be used object.issealed ()
4. I don't want others to add, remove members, or let others assign values to the members inside.
After using the Object.freeze () method, the assignment of person.age= "XXX" is invalid, but the Write property (defined above) is still valid
JS Learning Diary-Object literal