当前位置:网站首页>1. Deep copy 2. Call apply bind 3. For of in differences

1. Deep copy 2. Call apply bind 3. For of in differences

2022-07-07 13:22:00 Growing sunflower

1、Json.parse(Json.Stringfy()) Unable to implement functions and undefined Deep copy , How to solve it ?
Let's write a deep copy

  function deepClone(target) {
      if (target instanceof Object) {
        let dist
        if (target instanceof Array) {
          dist = []
        } else if (target instanceof Function) {
          dist = function () {
            return target.call(this, ...arguments)
          }
        } else {
          dist = {}
        }
        for (let key in target) {
          //  Filter out the attributes on the prototype 
          if (target.hasOwnProperty(key)) {
            dist[key] = deepClone(target[key])
          }
        }
        return dist
      } else {
        return target
      }
    }

2、call apply bind Use of methods

    var obj1 = {
      a: 2,
      b: 3,
      say: function (x, y) {
        return this.a + x + y
      },
    }
    var obj2 = {
      a: 3,
      b: 4,
    }
    var s1 = obj1.say.call(obj2, 5, 6)
    var s2 = obj1.say.apply(obj2, [7, 8])
    var s3 = obj1.say.bind(obj2)
    console.log(s1)
    console.log(s2)
    console.log(s3(1, 2))
    function f1(x, y) {
      console.log(x + y, this)
    }
    f1(10, 20) //30 undefined

    f1.call() //NaN undefined
    f1.apply() //NaN undefined

    f1.call(null, 2, 3) //5 null
    f1.apply(null, [2, 3]) //5 null

    var f2 = f1.bind(null)
    f2(2, 3) //5 null

    var obj = {
      age: 15,
      name: 'lalal',
    }
    f1.call(obj, 2, 3) //5 {age: 15, name: 'lalal'}
    f1.apply(obj, [2, 3]) //{age: 15, name: 'lalal'}
    var f2 = f1.bind(obj)
    f2(2, 3) //5 {age: 15, name: 'lalal'}

 Insert picture description here


    var person = {
  name: ' Zhang San ',
  describe: function () {
    return ' full name :'+ this.name;
  }
};

console.log(person.describe());// full name : Zhang San 
var A = {
  name: ' Zhang San ',
  describe: function () {
    return ' full name :'+ this.name;
  }
};

var B = {
  name: ' Li Si '
};

B.describe = A.describe;
console.log(B.describe());
// " full name : Li Si "
function f() {
  return ' full name :'+ this.name;
}

var A = {
  name: ' Zhang San ',
  describe: f
};

var B = {
  name: ' Li Si ',
  describe: f
};
console.log(A.describe()); // " full name : Zhang San "
console.log(B.describe()); // " full name : Li Si "
var obj1 = {
      name: 'zhangjing',
      aaa: function () {
        console.log(this.name)
      },
      bbb: function () {
        setTimeout(
          function () {
            this.aaa()
          }.apply(obj1),
          3000
        )
      },
    }
    obj1.bbb()//zhangjing

3、for of and for in difference

1、 Circular array
Difference one :for in and for of Circular arrays are OK ,for in The output is the of the array index Subscript , and for of The output is the value of each item of the array .

  const arr = ["orange", "apple", "banana", "pear"];
    for (let key of arr) {
      console.log(key); // orange  apple banana pear
    }
    for (let key in arr) {
      console.log(key); //0 1 2 3
    }
    for (const [key, value] of arr.entries()) {
      console.log(key, value); //0 'orange' 1 'apple' 2 'banana' 3 'pear'
    }

2、 Loop objects
Difference two :for in You can traverse objects ,for of Can't traverse object , Can only traverse with iterator Interface , for example Set,Map,String,Array

   const obj = { 1: "orange", 2: "apple", 3: "banana", 4: "pear" };
    for (let key of obj){
      console.log(key);// Error in created hook: "TypeError: [object Object] is not iterable!"

    }
    for (let key in obj) {
      console.log(key); // 1 2 3 4
      console.log(obj[key]); // orange  apple banana pear
    }

3. Array objects

  const fruits = [
      { 1: "orange" },
      { 2: "apple" },
      { 3: "banana" },
      { 4: "pear" },
    ];

    for (let value of fruits) {
      console.log(value); //{1: 'orange'}  {2: 'apple'}  {3: 'banana'} {4: 'pear'}
      for (let key in value) {
        console.log(key, value[key]);//1 orange  2 apple 3 banana 4 pear
      }
    }

summary :for in Suitable for traversing objects ,for of Suitable for traversing arrays .for in Traversing is the index of the array , Object properties , And the attributes on the prototype chain .

原网站

版权声明
本文为[Growing sunflower]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071126565465.html