JS object properties related to of check properties enumeration properties and so on

  • 2020-05-26 07:51:09
  • OfStack

1. Delete attributes

The delete operator can delete properties of an object


delete person.age // namely person No longer have properties age
delete person['age'] // Or it

delete just breaks the connection between the property and the host object, and does not manipulate the property in the property. After delete a.p, b.x is still 1


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 the own property, not the inherited property (to delete the inherited property, you must delete it from the prototype object that defines the property, which, of course, affects all objects that inherit from the prototype).


function inherit(p){ 
  if(p == null){  //  Not from null In the inheritance 
    throw TypeError();
  }
  if(Object.create){  // Use it if you have it 
    return Object.create(p);
  }
  var t = typeof p;
  if(t !== "object" || t !== "function"){  // The object to be inherited   The type should match 
    throw TypeError();
  }
  function f(){ }; // define 1 Two empty constructors 
  f.prototype = p; // The stereotype points to the object to be inherited p
  return new f();  // create f Object inherited 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, only configurable properties can use delete
Such as


delete Object.prototype; //  Can't delete   configurable 

var x = 1;
delete this.x; // Can't delete 
this.y = 1;
delete y; // So I can delete it 

function f(){ }
delete this.f; // Can't delete 

2. Detect attributes

The use of "in"

The in operator wants its left operand to be 1 string or can be converted to a string, and its right operand to be 1 object


var data = [5,6,7];
console.log("0" in data); // A subscript 0
console.log(1 in data);  //1 Can be converted to "1"
console.log("4" in data); // The subscript 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 Inherited this method 

Use hasOwnProperty() or propertyIsEnumerable() - the latter is an enhancement of the former
Continue to believe


var obj = {x:1};
console.log(obj.hasOwnProperty("x")); //true
console.log(obj.hasOwnProperty("y")); //false
console.log(obj.hasOwnProperty("toString")); //false  because obj Inherited this method , But not its own 

The latter returns true only if it is detected to be free and enumerable


var obj = {x:1};
console.log(obj.propertyIsEnumerable("x")); //true
console.log(obj.propertyIsEnumerable("y")); //false
console.log(obj.propertyIsEnumerable("toString")); //false  because obj Inherited this method , But not its own 
console.log(Object.prototype.propertyIsEnumerable("toString")); //false  Because the original  toString It's just not enumerable 

Of course, you can also use it directly! ==" operator


var obj = {x:1};
console.log(obj.x !== undefined);//true
console.log(obj.y !== undefined);//false
console.log(obj.toString !== undefined); //true 

3. Enumerate properties


var obj = {x:1,y:2};
for(p in obj){
 console.log(p);//x y
 console.log(obj.p);// undefined undefined
 console.log(obj[p]);//1 2
} 

Expand 1:

Each object has an associated stereotype (prototype), class (class), extensibility (extensible)
To detect whether one object is a prototype of another (or in a prototype chain), use the isPrototypeOf() method


var p = {x:1}; //p The prototype object is inherited from Object.prototype
var o = Object.create(p); //o Object inheritance 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 1 object, and its right operand to identify the object's class. If the object on the left is an instance of the class on the right, the expression returns true, otherwise false


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

Develop 2:

The class property of the object is a string representing the type information of the object

Call the toString() method and return the form of [object class]

Such as


var obj = {x:1,y:2};
console.log(obj.toString());//[object Object]

So to get the class of the object, you can find the "class" field in the returned string using slice (8, -1)
Such as


function inherit(p){ 
  if(p == null){  //  Not from null In the inheritance 
    throw TypeError();
  }
  if(Object.create){  // Use it if you have it 
    return Object.create(p);
  }
  var t = typeof p;
  if(t !== "object" || t !== "function"){  // The object to be inherited   The type should match 
    throw TypeError();
  }
  function f(){ }; // define 1 Two empty constructors 
  f.prototype = p; // The stereotype points to the object to be inherited p
  return new f();  // create f Object inherited 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
0

Related articles: