js you are not the object of those basic questions -Object

1 Object static method

  The so-called "static method" refers to deploy in Objectthe object itself.

1.1 Object.keys(),Object.getOwnPropertyNames()

  Object.keysMethods and Object.getOwnPropertyNamesmethods for traversing an object's properties.

  Object.keysParameter 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.getOwnPropertyNamesThe method and Object.keyssimilar, 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.keysMethod returns only the attributes enumerated (see "Object Object description attribute" chapter),

  Object.getOwnPropertyNamesThe 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 lengthproperties are not enumerated attributes.

  It appears only in the Object.getOwnPropertyNamesmethod of the returned results.

  In general, almost always used Object.keysmethod, traversing the object's properties.

1.2  Other methods

In addition to the above-mentioned two methods, Objectthere 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 of Prototypethe object.

2 Object Method Examples

  In addition to the static method, there are many methods defined in Object.prototypeobjects.

  They are called instances method, all Objectinstances of objects inherit these methods.

  ObjectMethod 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.hasOwnPropertyThe 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属性,所以返回truetoString属性是继承的,所以返回false

 

文章内容转自 阮一峰老师 JavaScript教程 https://wangdoc.com/javascript/index.html

 

Guess you like

Origin www.cnblogs.com/WernerWu/p/11354441.html