Object can be thought of as a class. There are 19 static methods on Object and 11 methods on prototype

    var o1 = new Object(a);var o2 = new Object(undefined);
    var o3 = new Object(null);
    console.log(o1);
    console.log(o2);
    console.log(o3);
Copy the code

Object.assign

If an attribute in the target object has the same key, the attribute is overwritten by an attribute in the source object. Properties of subsequent source objects similarly override properties of previous source objects.

The object. assign method copies only the enumerable properties of the source Object itself to the target Object. This method uses the source object’s [[Get]] and the target object’s [[Set]], so it calls the relevant getter and setter. Therefore, it assigns attributes, rather than just copying or defining new attributes. If the merge source contains getters, this may make it unsuitable to merge the new properties into the stereotype. To defined properties (including its enumerable) is copied to the prototype, the Object should be used. The getOwnPropertyDescriptor () and Object. The defineProperty ().

Both String and Symbol attributes are copied.

In the event of an error, for example, TypeError is raised if the property is not writable, and the target object can be changed if any properties are added before the error is raised. Note that Object.assign will not throw an error if the source Object is null or undefined.

Primitive types are wrapped as objects

const v1 = "abc";
const v2 = true;
const v3 = 10;
const v4 = Symbol("foo")

const obj = Object.assign({}, v1, null, v2, undefined, v3, v4);
// Primitive types are wrapped, null and undefined are ignored.
// Note that only string wrapping objects can have their own enumerable properties.
console.log(obj); // { "0": "a", "1": "b", "2": "c" }
Copy the code

Copy accessor

const obj = {
  foo: 1.get bar() {
    return 2; }};let copy = Object.assign({}, obj);
console.log(copy); // {foo: 1, bar: 2} copy. Bar comes from the return value of obj. Bar's getter function

// The following function copies the property descriptors of all its own properties
function completeAssign(target, ... sources) {
  sources.forEach(source= > {
    let descriptors = Object.keys(source).reduce((descriptors, key) = > {
      descriptors[key] = Object.getOwnPropertyDescriptor(source, key);
      return descriptors;
    }, {});

    // object. assign also copies enumerable Symbols by default
    Object.getOwnPropertySymbols(source).forEach(sym= > {
      let descriptor = Object.getOwnPropertyDescriptor(source, sym);
      if(descriptor.enumerable) { descriptors[sym] = descriptor; }});Object.defineProperties(target, descriptors);
  });
  return target;
}

copy = completeAssign({}, obj);
console.log(copy);
// { foo:1, get bar() { return 2 } }
Copy the code

Configuring property descriptors does not prevent objects from adding properties. Property descriptors are for properties of objects, whereas sealing or freezing is for objects

Object.create

Grammar:ObjectThe create (proto, [propertiesObject])Copy the code

The second argument needs to be passed as an attribute descriptor object. Null or string is an error, undefined is not an error, numeric or Boolean is not an error, but if the object is not an attribute descriptor object, it is an error

Object.defineProperty

DefineProperty Defines the property as 64X :false, and writes without any additional information. The property descriptor cannot be changed under any other circumstances. Once the 64X is set to false, the property cannot be deleted

Object.preventExtensions/Object.isExtensible

The object.preventExtensions () method makes an Object unextensible, meaning new properties can never be added. The return value is a reference to the original object, which is no longer extensible. The object.isextensible () method determines whether an Object isExtensible(whether new properties can be added to it). The return value is a Boolean value. This method makes the target object’s [[prototype]] immutable; Any reassignment [[prototype]] will raise TypeError. This behavior only applies to the internal [[prototype]] attribute; other attributes of the target object remain mutable. Once you have made an object unextensible, you can no longer make it extensible.

Object.seal/Object.isSealed

The object.seal () method seals an Object, preventing the addition of new properties and marking all existing properties as unconfigurable. The value of the current property can be changed as long as it was previously writable. The return value is a reference to the original object, which is already sealed. After enclosing an object, the attributes on the object’s prototype still inherit and the attributes on the prototype can be changed or added or deleted, but the __proto__ of the object cannot be changed to point to, That is, the prototype of the Object itself cannot be changed (because the change direction requires determining whether the Object itself isExtensible).

Object.freeze/Object.isFrozen

The object.freeze () method freezes an Object. A frozen object can no longer be modified. If you freeze an object, you cannot add new attributes to the object, delete existing attributes, modify the enumerability, configurability, writability of existing attributes of the object, or modify the value of existing attributes. In addition, the stereotype of an object cannot be modified after it is frozen. Freeze () returns the same object as the argument passed in. Object.freeze() makes the property descriptors of the properties in the Object unconfigurable, and sets all writable property descriptors of the properties that originally contain writable property descriptors to false. But the property that used to have a getter or setter function is actually made unconfigurable, but the getter or setter itself is not changed, which means that there is a way to freeze an object that the property can be changed, but only under special Settings

let abc = {}
let a = 1;
Object.defineProperty(abc, 'a', {
  get () {
    return a;
  },
  set (value) {
    a = value;
  },
  configurable: true
});

Object.freeze(abc); 
console.log(Object.isFrozen(abc));  //true
console.log(abc.a); / / 1
console.log(Object.getOwnPropertyDescriptor(abc, 'a'));  //{enumerable: false, different: false, get: ƒ, set: ƒ}
abc.a = 2;
console.log(abc.a); / / 2
Copy the code