1, object.keys () is used to iterate over the properties of the Object
var obj = {
key1: 1.key2: 2,}Object.keys(obj) // ['key1', 'key2']
Copy the code
2, delete command
Deletes object attributes and returns after the deletion is successfultrue
var obj = { p: 1 };
delete obj.p // true
obj.p // undefined
Object.keys(obj) / / []
Copy the code
Delete a nonexistent property, no error, and returntrue
var obj = {}
delete obj.p // trueThere's only one case where it comes backfalseObject attributes exist and cannot be deletedvar obj = Object.defineProperty({}, 'p', {
value: 1.configurable: false
});
delete obj.p // false
Copy the code
deleteYou can delete only properties of the object itself, not inherited propertiesvar obj = {}
delete obj.toString // true
obj.toString // function toString() { [native code] }
Copy the code
3. In operator
inThe operator checks whether an object contains an attribute and returns if it doestrueOtherwise returnfalse
inThe operator does not recognize which properties are its own and which are inherited properties. You can use the object's hasOwnProperty method to determine whether the properties are its ownvar obj = { p: 1 };
'p' in obj //true
'toString' in obj // true
Copy the code
4, for… The in traverse
for. In is used to iterate over all properties of an object with two caveats (1). It traverses all traversable properties and skips non-traversable properties (2). It iterates not only over the properties of the object itself, but also over inherited propertiesvar obj = { a: 1.b: 2.c: 3 };
for (var i in obj) {
console.log(i); // a b c} You can combine the hasOwnProperty method when you just want to iterate over the properties of the object itselfvar obj = { name: 'zhangsan' };
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
console.log(key) // name}}Copy the code
Object.prototype.valueof (), which returns the Object itself by default
var obj = { a: 1 };
obj.valueOf() // { a: 1 }
obj.vlaueOf() === obj // true
Copy the code
6, the Object. The prototype. The toString () method, by default the return type string
var obj = {};
obj.toString() // "[object Object]"
Copy the code
Arrays, strings, functions,DateObjects are deployed with custom toString methods, overriddenObject. Prototype. ToString method. [1.2.3].toString() / / "1, 2, 3"
var f = function () {}
f.toString() // "function () {}"
(new Date()).toString() // "Mon May 24 2021 16:24:41 GMT+0800"
Copy the code
Object. The prototype. ToString. Call (value) can be accurate to judge what is a value type
Value: Returns [objectNumber] String: Returns [objectString] Boolean: returns [objectBoolean]
undefinedReturn [object Undefined]null: Returns [object Null] array: returns [objectArray]
argumentsFunction: Returns [object]Function]
ErrorObject: Returns [objectError]
DateObject: Returns [objectDate]
RegExpObject: Returns [objectRegExp] Other objects: Return [objectObject]
Object.prototype.toString.call(2) // "[object Number]"
Object.prototype.toString.call(' ') // "[object String]"
Object.prototype.toString.call(true) // "[object Boolean]"
Object.prototype.toString.call(undefined) // "[object Undefined]"
Object.prototype.toString.call(null) // "[object Null]"
Object.prototype.toString.call(Math) // "[object Math]"
Object.prototype.toString.call({}) // "[object Object]"
Object.prototype.toString.call([]) // "[object Array]"Using this property, you can write a ratiotypeofOperator a more accurate type determination functionvar type = function (obj) {
var t = Object.prototype.toString.call(obj);
return t.match(/\[object (.*?)\]/) [1].toLowerCase();
}
type({}); // "object"
type([]); // "array"
type(5); // "number"
type(null); // "null"
type(); // "undefined"
type(/abcd/); // "regexp"
type(new Date()); // "date"
Copy the code
7, the Object. The prototype. The hasOwnProperty () method takes a string as a parameter, returns a Boolean value, whether this instance Object itself has the attribute.
var obj = { p: 1 }
obj.hasOwnProperty('p') // true
obj.hasOwnProperty('toString') // false
Copy the code
8. Property description object
{
// value is the attribute value of this attribute. The default value is undefined
value: 123.// Writable is a Boolean value indicating whether the property value value can be changed. The default value is true
writable: true.// Enumerable is a Boolean that indicates whether the property is iterable. The default is true
enumerable: true.// Signals is a Boolean value indicating the configurability of any device. The default value is true
configurable: true.// get is a function that represents the getter for the property. The default value is undefined
get: undefined.// Set is a function that represents a setter for this property. The default is undefined
set: undefined
}
Copy the code
9, Object. GetOwnPropertyDescriptor () method can obtain property description Object. Its first argument is the target object, and its second argument is a string corresponding to the name of an attribute of the target object.
let obj = { p: 'a' };
Object.getOwnPropertyDescriptor(obj, 'p')
// Object { value: "a",
// writable: true,
// enumerable: true,
// configurable: true
// }
ObjectGetOwnPropertyDescriptor () method can only be used for the properties of the object itself, cannot be applied to inherit propertylet obj = { p: 'a' };
Object.getOwnPropertyDescriptor(obj, 'toString') // undefined
Copy the code
The Object.defineProperty() method allows you to describe an Object by attribute, define or modify an attribute, and then return the modified Object
/ / usage
Object.defineProperty(object, propertyName, attributesObject)
ObjectDefineProperty takes three parameters as follows: Object Object property propertyName A character string that indicates the name of the property attributesObject Describes the object of the propertylet obj = Object.defineProperty({}, 'p', {
value: 123.writable: false.enumerable: true.configurable: true
})
obj.p / / 123
obj.p = 456;
obj.p / / 123
Copy the code
Accessors: Store functions called setters use properties to describe set properties of objects. The value function, called a getter, uses properties to describe the get property of an object.
let obj = {
get p() {
return 'getter';
},
set p(value) {
console.log('setter:' + value)
}
}
obj.p // 'getter'
obj.p = 123 // setter:123
// The value function get can't take arguments. The value function set can only take one argument.
Copy the code