JS object attributes (check attributes, enumeration attributes, etc)
1. Delete attributes
The delete operator can delete object attributes.
The Code is as follows:
Delete person. age // that is, the person no longer has an attribute age.
Delete person ['age'] // or
Delete only disconnects the property from the host object, and does not operate the property. After delete a. p, B. x is still 1.
1 2 3 4 5 6 7 |
Var a = {p: {x: 1 }}; Var B = a. p; Console. log (a. p. x); // 1 Delete a. p; Console. log (a. p. x); // TypeError a. p is undefined Console. log (a. p); // undefined Console. log (B. x); // 1 |
Delete can only delete its own attributes, but cannot delete the inherited attributes. (To delete the inherited attributes, you must delete them from the prototype object that defines this attribute. Of course, this will affect all objects inherited from this prototype)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Function inherit (p ){ If (p = null) {// cannot inherit from null Throw TypeError (); } If (Object. create) {// use this method directly Return Object. create (p ); } Var t = typeof p; If (t! = "Object" | t! = "Function") {// The object type to be inherited must match Throw TypeError (); } Function f () {}; // defines an empty constructor. F. prototype = p; // prototype indicates the object to be inherited. Return new f (); // create an f object, which inherits from p } Var obj = {x: 1 }; Var obj1 = inherit (obj ); Obj1.y = 2; Console. log ("x =" + obj1.x + "y =" + obj1.y); // x = 1 y = 2 Delete obj1.x; Delete obj1.y; Console. log ("x =" + obj1.x + "y =" + obj1.y); // x = 1 y = undefined |
Of course, you can use delete only for configurable attributes.
For example
1 2 3 4 5 6 7 8 9 |
Delete Object. prototype; // cannot be deleted and cannot be configured Var x = 1; Delete this. x; // cannot be deleted This. y = 1; Delete y; // delete Function f (){} Delete this. f; // cannot be deleted |
2. Check attributes
Use "in"
The in operator wants its left operand to be a string or can be converted to a string. It wants its right operand to be an object.
1 2 3 4 5 6 7 8 9 |
Var data = [5, 6, 7]; Console. log ("0" in data); // There are subscript 0 Console. log (1 in data); // 1 can be converted to "1" Console. log ("4" in data); // The subscript is only 1 2 3. Var obj = {x: 1 }; Console. log ("x" in obj); // true Console. log ("y" in obj); // false Console. log ("toString" in obj); // true because obj inherits this method |
Use hasOwnProperty () or propertyIsEnumerable () --- the latter is the enhancement of the former.
Gu mingyu
1 2 3 4 |
Var obj = {x: 1 }; Console. log (obj. hasOwnProperty ("x"); // true Console. log (obj. hasOwnProperty ("y"); // false Console. log (obj. hasOwnProperty ("toString"); // false because obj inherits this method, but not its own |
The latter returns true only when it detects a free attribute and an enumerable attribute.
1 2 3 4 5 |
Var obj = {x: 1 }; Console. log (obj. propertyIsEnumerable ("x"); // true Console. log (obj. propertyIsEnumerable ("y"); // false Console. log (obj. propertyIsEnumerable ("toString"); // false because obj inherits this method, but not its own Console. log (Object. prototype. propertyIsEnumerable ("toString"); // false because the original toString cannot be enumerated |
Of course, you can also use "! = "Operator judgment
1 2 3 4 |
Var obj = {x: 1 }; Console. log (obj. x! = Undefined); // true Console. log (obj. y! = Undefined); // false Console. log (obj. toString! = Undefined); // true |
3. Enumeration Properties
1 2 3 4 5 6 |
Var obj = {x: 1, y: 2 }; For (p in obj ){ Console. log (p); // x y Console. log (obj. p); // undefined Console. log (obj [p]); // 1 2 } |
Expansion 1:
Each object has prototype, class, extensible)
To check whether an object is a prototype of another object (or is in the prototype chain), you can use the isPrototypeOf () method.
1 2 3 4 5 6 |
Var p = {x: 1}; // The prototype Object of p inherits from Object. prototype. Var o = Object. create (p); // The o Object inherits from p Console. log (p. isPrototypeOf (o); // true Console. log (Object. prototype. isPrototypeOf (o); // true Console. log (Object. prototype. isPrototypeOf (p); // true |
Of course, the isPrototypeOf () method is very similar to the instanceof operator.
The instanceof operator wants its left operand to be an object, and the right operand to identify the class of the object. If the object on the left is an instance of the right class, the expression returns true; otherwise, false.
1 2 3 4 5 6 7 |
Var p = {x: 1 }; Console. log (p instanceof Object); // true Var d = new Date (); Console. log (d instanceof Date); // true Console. log (d instanceof Object); // true Console. log (d instanceof Number); // false |
Expansion 2:
The class attribute of an object is a string used to indicate the object type information.
Generally, after the toString () method is called, the returned result is in the form of [object class ].
For example
The Code is as follows:
Var obj = {x: 1, y: 2 };
Console. log (obj. toString (); // [object Object]
Therefore, to obtain the class of the object, you can find the "class" field in the returned string and use slice (8,-1)
For example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
Function classOf (obj) {// find the class name If (obj = null ){ Return "Null "; } If (obj = undefined ){ Return "Undefined "; } Return Object. prototype. toString. call (obj). slice (8,-1 ); } Console. log (classOf (1); // Number // Note that these types of variables call the toString () method instead of directly calling the toString () method. // Console. log (1. toString (); // an error is returned. Var a = 1; Console. log (Number (1). toString (); // 1 Console. log (a. toString (); // 1 Console. log ({x: 1}. toString (); // [object Object] Console. log (classOf (1); // Number Console. log (classOf (""); // String Console. log (classOf ("str"); // String Console. log (classOf (null); // Null Console. log (classOf (false); // Boolean Console. log (classOf ({}); // Object Console. log (classOf ([]); // Array Console. log (classOf (new Date (); // Date Function f (){} Console. log (classOf (new f (); // Object |