1 Object static method
The so-called "static method" refers to deploy in Object
the object itself.
1.1 Object.keys(),Object.getOwnPropertyNames()
Object.keys
Methods and Object.getOwnPropertyNames
methods for traversing an object's properties.
Object.keys
Parameter is an object method returns an array.
Members of the array is the object itself (rather than inherited) all the property names.
var obj = {
p1: 123,
p2: 456
};
Object.keys(obj) // ["p1", "p2"]
Object.getOwnPropertyNames
The method and Object.keys
similar, but also takes an object as a parameter,
It returns an array containing all the attributes of the object name itself.
For general objects, Object.keys()
and Object.getOwnPropertyNames()
returns the result is the same.
When only involves non-enumerable properties will have different results.
Object.keys
Method returns only the attributes enumerated (see "Object Object description attribute" chapter),
Object.getOwnPropertyNames
The method also returns the attribute name can not be enumerated.
var a = ['Hello', 'World'];
Object.keys(a) // ["0", "1"]
Object.getOwnPropertyNames(a) // ["0", "1", "length"]
The above code, the array length
properties are not enumerated attributes.
It appears only in the Object.getOwnPropertyNames
method of the returned results.
In general, almost always used Object.keys
method, traversing the object's properties.
1.2 Other methods
In addition to the above-mentioned two methods, Object
there are many other static methods, one by one will be described in detail later.
Related methods (1) the model object properties
Object.getOwnPropertyDescriptor()
: For a description of the object of a property.Object.defineProperty()
: By describing the object, define a property.Object.defineProperties()
: Description object that defines a plurality of attributes.
(2) a method of controlling the state of the object
Object.preventExtensions()
: Object prevent expansion.Object.isExtensible()
: Determining whether the object is extended.Object.seal()
: Prohibited object configuration.Object.isSealed()
: Determining whether an object is disposed.Object.freeze()
: A frozen object.Object.isFrozen()
: To determine whether an object is frozen.
(3) Method prototype chain-related
Object.create()
: The method may specify the object and property of the prototype, a new object is returned.Object.getPrototypeOf()
: Gets the object ofPrototype
the object.
2 Object Method Examples
In addition to the static method, there are many methods defined in Object.prototype
objects.
They are called instances method, all Object
instances of objects inherit these methods.
Object
Method instance of an object, mainly in the following six.
Object.prototype.valueOf()
: Returns the value of the current corresponding to the object.Object.prototype.toString()
: Returns the object corresponding to the current string.Object.prototype.toLocaleString()
: Returns the local string corresponding to the current object.Object.prototype.hasOwnProperty()
: To determine whether a property is the current object's own properties, or inherited property from the prototype object.Object.prototype.isPrototypeOf()
: Determining whether the current prototype of an object to another object.Object.prototype.propertyIsEnumerable()
: To determine whether a property enumerable.
2.1 Object.prototype.hasOwnProperty()
Object.prototype.hasOwnProperty
The method takes a string as a parameter,
It returns a Boolean value that indicates whether the object instance itself has the property.
var obj = {
p: 123
};
obj.hasOwnProperty('p') // true
obj.hasOwnProperty('toString') // false
上面代码中,对象obj
自身具有p
属性,所以返回true
。toString
属性是继承的,所以返回false
。
文章内容转自 阮一峰老师 JavaScript教程 https://wangdoc.com/javascript/index.html