How do you delete an object property or method

Use the delete key, property or method of the object can be deleted.

var obj = {};
obj.name; // undefined;

obj.name = "Lilei";
obj.name; // "Lilei"

obj.sayHello = function () { console.log("hello") };
obj.sayHello(); // "Hello";

delete obj.name; // true
delete obj.sayHello; // true;

obj.name; // undefined;
obj.sayHello(); // Error: undefined

 

From the above, you can use delete to delete an object property or method, delete successful returns true, will return undefined when there is no reference to the attributes, will complain when a method call that does not exist.

Guess you like

Origin www.cnblogs.com/aisowe/p/11640928.html