In JavaScript, almost all objects are instances of type Object, and they inherit properties from Object.prototype.

Object is a built-in Object in JavaScript. It is a constructor, but it can also be used as a normal function.

The constructor

Object as a constructor, you can use the new keyword to generate a new Object.

// This is an empty object
var obj = new Object(a);Copy the code

Object can take one argument:

  • If the parameter isnullorundefined, returns an empty object.
  • If the argument is an object, the object is returned.
  • If the argument is a primitive type value, return a wrapper object for that value.
var o1 = new Object(null); / / {}
var o2 = new Object(undefined); / / {}

var o3 = {a: 1};
var o4 = new Object(o3);
o3 === o4 // true

var obj = new Object(123);
obj instanceof Number // true
Copy the code

Common function

Object is a common function that converts any value into an Object. The effect is the same except that the semantics differ from constructors.

// If there is no argument, or if the argument is' null 'or' undefined ', an empty object is returned.
var o1 = Object(a);/ / {}
var o2 = Object(undefined); / / {}
var o3 = Object(null); / / {}

o1 instanceof Object // true

// If the argument is an object, the object is returned without conversion.
var arr = [];
var obj = Object(arr); // Return the original array
obj === arr // true

// If the parameter is a primitive type value, it is converted to the corresponding wrapper object.
var obj = Object(1);
obj instanceof Object // true
obj instanceof Number // true
Copy the code

Constructor method

The Object constructor contains functions in its own properties that are similar to static methods in Java and can be called using the object.xxx () form.

View all properties of the Object constructor:

Object.getOwnPropertyNames(Object);
// ["length", "name", "prototype", "assign", "getOwnPropertyDescriptor", "getOwnPropertyDescriptors", "getOwnPropertyNames", "getOwnPropertySymbols", "is", "preventExtensions", "seal", "create", "defineProperties", "defineProperty", "freeze", "getPrototypeOf", "setPrototypeOf", "isExtensible", "isFrozen", "isSealed", "keys", "entries", "fromEntries", "values"]
Copy the code

Methods related to object attributes:

  • Object.keys(): Gets the property name of the object
  • Object.getOwnPropertyNames(): Gets the property name of the object.
  • Object.getOwnPropertyDescriptor(): Gets the description object of an attribute.
  • Object.defineProperty(): Defines an attribute by describing an object.
  • Object.defineProperties(): Defines multiple properties by describing objects.

Object. The keys () and the Object. GetOwnPropertyNames () are two functions return an Object property names, the difference is that the Object. The keys () only returns can be enumerated attribute names.

var a = ['Hello'.'World'];

Object.keys(a) / / / "0", "1"
Object.getOwnPropertyNames(a) // ["0", "1", "length"]
Copy the code

Methods for controlling object state:

  • Object.preventExtensions(): Prevents object expansion.
  • Object.isExtensible(): Determines whether an object is extensible.
  • Object.seal(): Prevents other code from deleting attributes of the object.
  • Object.isSealed(): Determines whether an object is configurable.
  • Object.freeze(): Freezes an object.
  • Object.isFrozen(): Determines whether an object is frozen.

Prototype chain correlation method:

  • Object.create()This method returns a new object by specifying a prototype object and properties.
  • Object.getPrototypeOf() : Gets the prototype object of the specified object.

Constructor prototype

In JavaScript, object inheritance takes a prototype-based approach, and the prototype property refers to the prototype object.

That is, the __proto__ of the new Object comes from the prototype property of the Object constructor.

var obj = new Object(a); obj.__proto__ ===Object.prototype; // true
Copy the code

All objects inherit properties from the prototype Object pointed to by Object.prototype, and when a new property is added to the prototype Object, all objects can access that property.

Object.prototype.newP = 'newV';
var obj = new Object(a); obj.newP;// newV
Copy the code

The six main methods in Object.prototype are:

  • Object.prototype.valueOf(): Returns the value of the current object.
  • Object.prototype.toString(): Returns the string representation of the current object.
  • Object.prototype.toLocaleString(): Returns the local string representation of the current object.
  • Object.prototype.hasOwnProperty(): Determines whether an attribute is a property of the current object itself or inherits from the prototype object.
  • Object.prototype.isPrototypeOf(): Determines whether the current object is a prototype of another object.
  • Object.prototype.propertyIsEnumerable(): Determines whether an attribute is enumerable.

These methods are inherited by all Object objects.

var num = new Number(1);
num instanceof Object; // true
num.valueOf(); / / 1
Copy the code

The relevant data

19.1.1 The Object Constructor

JavaScript standard built-in Object -Object

Object