JS面试知识点10-手写代码

  • call、apply、bind函数内部实现是怎么样的

    /*
    参数,
    调用函数,
    返回值,
    */
    Function.prototype.myCall = function (obj, ...args) {
      if (typeof this !== 'function') {
        throw new Error('not a function')
      }
      const that = obj || window;
      that.fn = this;
      const result = that.fn(...args);
      delete that.fn;
      return result;
    }

    Function.prototype.myApply = function (obj, arr = []) {
      if (typeof this !== 'function') {
        throw new Error('not a function')
      }
      const that = obj || window;
      that.fn = this;
      const result = that.fn(...arr);
      delete that.fn;
      return result;
    }

    function test(...jobs) {
      this.job = jobs;
      console.log(this);
      return this.age;
    };
    let obj = { name: 'lisa', age: 12 };
    var arr = ['job', 'gender']
    const a = test.myCall(obj);
    const b = test.myApply(obj, arr);
    console.log(a, b);
  • 为什么0.1 + 0.2 != 0.3 如何解决;
  •  

猜你喜欢

转载自blog.csdn.net/weixin_43374360/article/details/109182820