The object properties in JS can be added and deleted dynamically. Delete The object property with the delete keyword.
function obj () { } varnew obj (); = "a"; = "B"; // output a // Output B Delete oo.a; // output undefined
In JS, the properties of an object are represented by key-value pairs, and are unordered (over time). The attribute name ( i.e. key) in JS can be accessed ( or assigned) with a dot, or it can be accessed ( or assigned ) with the Bracket key value (the key must be of type string when accessing the object property with the bracket key) )
var a = {x:5,y:2//a[' x ']; Output 5
There are two ways to traverse object properties, which are commonly used to traverse object properties.
var obj = {a1:1, a2:2, A3:3, A4:4, A5:5 }; for (var i=1;i<=5;i++ ) { console.log (obj[' a ' + i]);} // Output 1 2 3 4 5
var obj = {a:1, b:2, C:3, D:4, E:5 }; for (var in obj) { console.log (Obj[item]);} // Output 1 2 3 4 5
It is important to note that for-in traversal object properties are unordered and may traverse properties on the prototype chain. And you can only use the [] Read property when using for in traversal, you cannot use. read.
functionObj_pro () {}//creating the Obj_pro constructorOBJ_PRO.PROTOTYPE.F = 55;//Create Obj_pro prototype properties Fvarobj =NewObj_pro ();//Obj_pro Creating an Obj object//obj object property Loop Assignment for(vari=1;i<=5;i++) {obj[' A ' + i] = (i+9);}//traversing obj Object Properties for(varIteminchobj) {Console.log (Obj[item]);}//Output 10 11 12 13 14 55 Here's a traversal of the property F on the prototype chain .
*************************************************************************************************************** *
Get/set of attributes
Get/set, like other languages, is used to encapsulate object properties. Get settings read, set set write.
var person = { name:' Mirage ', get Age () { return age ; }, set Age (val) { if(val < 0 | | IsNaN (val) | | val >) { =; } Else{ = val; } = -99; console.log (person.age); // Output
The value of age can be judged in set, as well as in get.
var person = { name:' Mirage ', get Age () { if' < 0 | | IsNaN (age) | | Age >) { return ; } Else { return Age } }, set Age (val) { = val; = -99; console.log (person.age); // Output
You can set specific properties to read-only or unreadable through Get/set
var player = { get Gameyear () { return 5; =Console.log (player.gameyear); // Output 5
Only the Get return value is set, and the property is not modifiable ( read-only ).
var player = { set Age (val) { if(val < 0 | | IsNaN (val) | | val >) { =; } Else { = val; } = "Age"; // player.pname value isconsole.log (player.age); // output undefined
Set only, this property can be modified but not readable.
*************************************************************************************************************** **
The Get/set of the prototype chain
Object.defineproperty is required to set the Get/set property for the object created by the constructor.
The object.defineproperty function can add object properties and modify the attributes of an existing property.
object.defineperperty Parameters:
Object (required): The target object (which can also be added to the object's prototype, as well as a DOM object). type:ObjectName(object name).
PropertyName (required): property name. type:String (string).
Descriptor (required): Property descriptor. type:Object _ literal.
functionPerson () {}object.defineproperty (Person.prototype,"Age", {get:function(){ returnAge ; }, set:function(val) {if(val < 0 | | | IsNaN (val) | | val > 150) { age= 18; }Else{ Age=Val; } } });varP1 =NewPerson ();p 1.age= 6150; Console.log (p1.age); //output 18.
The object prototype properties can be modified by DefineProperty.
JS's OOP < two > object properties