This is the 9th day of my participation in Gwen Challenge

A static method

Object.assign()

ES6 new

  1. usage

    Merge or blend objects.

    const target = { a: 1.b: 2 };
    const source1 = { b: 3.c: 4 };
    const source2 = { b: 5.c: 6 };
    Object.assign(target, source1, source2);
    Copy the code

    The attributes in source1 are first mixed into the target; duplicate attributes are overwritten. Add the attribute of the next parameter, source2, to target and repeat the override. Finally, target is returned.

    target
    //  {
    // a: 1,
    // b: 5,
    // c: 6
    / /}
    Copy the code
  2. details

    • Only source properties are copied, not the prototype, and are enumerable, including symbolic properties.

      Source. The hasOwnProperty (” b “) and the source. PropertyIsEnumerable (” a “) will return true.

      Of course, symbolic properties are also copied, although symbolic properties cannot be traversed by for-in, but both functions return true.

    • Target’s set and source’s GET are called.

    • The get and set of accessor properties are not copied.

    • If an error is thrown during the call to Object.assign(), the mixin operation is not rolled back and may only be half copied.

    • Shallow copy, if the property is of reference type, only its reference is copied.

    • The order of enumeration is deterministic, enumerating numeric keys in ascending order before enumerating strings and symbolic keys in insertion order.

    • 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
  3. role

    Object.assign({}, obj1, obj2);
    Copy the code

    When creating a new object, you can copy the properties of obj1 and obj2 over.

    Object.assign(oldObj, newObj);
    Copy the code

    You can update the value of an existing property in an object.

    {... obj1, ... obj2}Copy the code

    Can achieve the same effect.

Object.is()

ES6 added. Judge equal values to be equal.

Before ES6, there were things the == operator couldn’t do.

NaN= =NaN     // false
+0= = -0      // true
Copy the code

It doesn’t make sense.

Object.is(NaN.NaN);   // true
Object.is(+0, -0);     // false
Copy the code

That makes sense.

Object.keys()

Pass in an object and return an array consisting of the enumerable property names of the object. Does not contain the attribute name on the stereotype, nor does it contain the symbolic attribute name.

const person = {
    name: "herb".// Common attributes
    [Symbol("age")]: 18.// Symbolic attributes
    high: "1.8 m"          // Non-enumerable attributes
}
Object.defineProperty(person, "high", { enumerable: false });
person.__proto__.father = "hhhhhh";    // Attributes on the prototype
Copy the code
Object.keys(person);
// ["name"]
Copy the code

The array contains no symbols or non-enumerable properties.

Object.keys("abcd");
// ["0", "1", "2", "3"]
Copy the code

ES6 previously reported an error when passing a raw value, but now converts the original type to an object and processes it.

Object.values()

Pass in an object and return an array of the enumerable property values of the object. Contains no property values on the stereotype, and no symbol property values.

Object.values(person);
// ["herb"]
Copy the code
Object.values("abc");
// ["a", "b", "c"]
Copy the code

Parameters with raw values are converted to object processing.

Object.entries()

Returns a two-dimensional array. Each one-dimensional array has two elements, key-value pairs. Contains no key-value pairs on the prototype, and no symbolic key-value pairs.

Object.entries(person);
// [ ["name", "herb"] ]
Copy the code
Object.entries("abc");
// [ ["0", "a"], ["1", "b"], ["2", "c"] ]
Copy the code

The in operator

  1. for-in

    Whether the property is on an instance or a stereotype, as long as it is enumerable.

    for(let prop in person) {
        console.log(prop);
    }
    // name
    // father
    Copy the code

    Iterates through all enumerable property names on the object and its stereotype, excluding symbolic properties.

    for(let prop in person) {
        console.log(person[prop]);
    }
    // herb
    // hhhhhh
    Copy the code
  2. in

    Returns true if the property exists, whether it is on an instance or stereotype, whether it can be enumerated or not.

    "name" in person   // true
    "father" in person // true
    Copy the code

Object.getOwnPropertyNames()

Pass in an object and return an array of all the property names (enumerable or not) on the object, not including symbolic properties, not including those on the stereotype.

Object.getOwnPropertyNames(person);
// ["name", "high"]
Copy the code

High is also returned as a non-enumerable property.

Object.getOwnPropertyNames("abc");
// ["0", "1", "2", "length"]
Copy the code

The original value is converted to an object.

const obj = {
   "31": "d"."0": "a"."4": "e".c:  "c".b:  "b"
}
Object.getOwnPropertyNames(obj)
// ["0", "4", "31", "c", "b"]
Copy the code

The keys in the object are sorted by numeric size, if any, and in order of precedence.

Object.getOwnPropertySymbols()

Pass in an object and return an array of all its own symbolic attribute names (whether or not enumerable), excluding those on the stereotype.

const sym = {
    [Symbol("s1")]: "s1"[Symbol.for("s2")]: "s2"[Symbol.iterator]() {
        return {
            next() {
                return {
                    value: 1.done: false
                }
            }
        }
    }
}
Copy the code
Object.getOwnPropertySymbols(sym);
// [Symbol(s1), Symbol(s2), Symbol(Symbol.iterator)]
Copy the code

Object.getPrototypeOf

Pass in an object and return its implicit prototype.

Object.getPrototypeOf({}) === Object.prototype
// true
Copy the code

{} is created by Object, so the implicit prototype for {} points to Object.prototype.

Using the __porto__ attribute on an object to retrieve implicit stereotypes is no longer recommended.

setPrototypeOf()

Pass in an object A and an object B, and change the stereotype of object A to object B.

Object.setPrototypeOf(obj, prototype)
Copy the code

For details, see MDN. Object.create() is recommended.

Object.create()

Pass in an object and create a new object whose implicit prototype is the object passed in.

See MDN for examples

Object.create(null);

let obj = {};
obj.__proto__ = null;
Copy the code

Pass a NULL and create an empty object with no prototype and no attributes. The two notations above give the same effect.

Object.create({}, {
    prop: {
        value: "hhhh"}})Copy the code

If two arguments are passed in, the created Object and the second argument are the equivalent of passing Object.defineProperties().

This is equivalent to creating an object and assigning a value.

function inherit(father, son) {
    return son.prototype = Object.create(father.prototype, {
        constructor: {
            value: son,
            writable: true.configurable: true}})}Copy the code

You can do inheritance in this way, father is the parent of son.

Instance methods

hasOwnProperty()

Pass in a parameter that determines whether the property exists on the instance and returns a Boolean value. Not including the prototype.

const obj = {};
obj.hasOwnProperty("constructor");               // false
Object.prototype.hasOwnProperty("constructor");  // true
Copy the code

isPrototypeOf()

Pass in an object and determine if there are any objects in the prototype chain that call this function.

const obj = {};
Object.prototype.isPrototypeOf(obj);   // true
Copy the code

Obj must have Object. Prototype on its prototype chain.

obj.isPrototypeOf(Object.prototype);   // false
Copy the code

Object. Prototype: no Object.

propertyIsEnumerable()

Passing in a parameter determines whether the parameter is enumerable in the object.

const person = {
    name: "herb".// Common attributes
    [Symbol.for("age")]: 18.// Symbolic attributes
    high: "1.8 m"              // Non-enumerable attributes
}
Object.defineProperty(person, "high", { enumerable: false });
Copy the code
person.propertyIsEnumerable("name");             // true
person.propertyIsEnumerable(Symbol.for("age"));  // true
person.propertyIsEnumerable("high");             // false
Copy the code