This is the first day of my participation in the Gwen Challenge in November. Check out the details: the last Gwen Challenge in 2021

Haven’t written an article for a long time, just borrow more text challenge urge oneself study. Recently has been busy autumn recruit, feel their precipitation for technology or a little less, set a small target for yourself.

Recently when learning JS encountered a lot of handwritten array, object, string, function method, has not been summed up. Thinking of writing an article summary, just can consolidate knowledge.

1, the Object. Assign

  • define

Oject.assign(target,… Sources is used to assign the value of an enumerable property from one or more source objects to a target object, which will return the target object.

Simply put, it is possible to make a copy of a value, often used in shallow copies of objects.

For example, copy object A to object B

    const objectA = {
        name: 'ObjectA'.age: 18
    };
    const objectB = {
        name: 'ObjectB'.age: 20
    };

    const returnedTarget = Object.assign(objectA, objectB);

    console.log(objectA); // {name: 'ObjectB', age: 20}
    console.log(objectB); // {name: 'ObjectB', age: 20}
    console.log(returnedTarget); // {name: 'ObjectB', age: 20}
Copy the code

As you can see from the above code, when object A is copied to object B, object A itself changes. This is a shallow copy of the classic.

  • Implementation method

As you can see from the above usage, the main object is to copy the source object to the target object, and object copy is not to copy the basic data type, just copy the value.

Here we define methods on objects using Object.defineProperty(). The Object.defineProperty() method directly defines a new property on an Object, or modifies an existing property of an Object, and returns the Object.

Object.defineProperty(Object.'assign', {
    value: function(target, ... args) {
        if (target == null) {
            return new TypeError('Cannot convert undefined or null to object');
        }
        // uniformly reference data types
        const to = Object(target);

        for (let i = 0; i < args.length; i++) {
            // Each source object
            const nextSource = args[i];
            if(nextSource ! = =null) {
                / / used for... In and hasOwnProperty check to ensure that only its own properties and methods are retrieved
                for (const nextKey in nextSource) {
                    if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) { to[nextKey] = nextSource[nextKey]; }}}}return to;
    },
    // Not enumerable
    enumerable: false.// Attribute values cannot be modified
    writable: true.// Attribute descriptors cannot be changed
    configurable: true,})Copy the code

1. First, specify the object passed in: target object, source object

2. Check whether the target object is empty

3. Uniform target objects are reference data types

4, Pass through the source object using for… In and hasOwnProperty, get its own properties and methods, and finally return to the target formation

2, the Object is

  • define

Oject.is(target,… Sources is used to determine whether two values are the same.

Although object methods, values can be judged by more than just objects.

The following two values can be determined:

  • The values passed in are undefined

  • All values passed in are NULL

  • The values passed in are either true or false

  • The value passed in is the same string

  • The value passed in is the same object (that is, the reference address is the same)

  • The values passed in are all numbers and are all +0 or -0 or NaN or some other number

We can test this:

    let objectA = {
        name: '1'.age: 12
    };
    let objectB = {
        name: '1'.age: 12
    }
    let objectC = objectA
    / / 1
    console.log(Object.is(undefined.undefined)); // true
    console.log(Object.is(null.null)); // true
    console.log(Object.is(true.true)); // true
    console.log(Object.is(true.false)); // false
    console.log(Object.is('str'.'str')); // true
    / / 2
    console.log(Object.is(objectA, objectB)); // false
    console.log(Object.is(objectA, objectC)); // true
    / / 3
    console.log(Object.is(0.0)); // true
    console.log(NaN= = =NaN); // false
    console.log(Object.is(NaN.NaN)); // true
    console.log(+0= = = -0); // true
    console.log(Object.is(+0, -0)); // false
Copy the code

There is nothing to be said for the first part, which is the ordinary judgment of equality; Object is returns false. Object. Is if two objects have the same contents but different reference addresses.

In ordinary equality notation, NaN === NaN returns false, which is not what we want, and +0 and -0, which in our minds should not be equal.

So the implementation of this method is mainly to solve this problem.

The implementation code is posted below:

const is = (x, y) = > {
    if (x === y) {
        returnx ! = =0|| y ! = =0 || 1 / x === 1 / y
    } else {
        returnx ! == x && y ! == y } }Copy the code