js高级进阶——手动实现apply()函数

news/2024/7/19 15:27:51 标签: js, apply, 高级进阶
    // 首先看下apply()
    var obj = {
      name: 'Nicholas S.Zakas',
      introduce: function (from) {
        console.log('hello, everyone, i am ' + this.name, 'i am from ' + from)
      }
    }

    var obj2 = {
      name: 'Evan You'
    }

    obj.introduce.apply(obj2, ['china']) // hello, everyone, i am Evan You i am from china

    Function.prototype._apply = function (context = window, arr) {
      var result
      context.fn = this
      if (!arr) { // 考虑无参数时
        context.fn()
      } else {
        var args = []
        for (var i = 0; i < arr.length; i++) {
          args.push('arr[' + i + ']')
        }
        result = eval('context.fn(' + args + ')')
      }
      delete context.fn
      return result
    }

    obj.introduce._apply(obj2, ['Evan You', 'china']) // hello, everyone, i am Evan You i am from china

    // ECMAScript6的实现
    Function.prototype._es6apply = function (context = window, args) {
      let result
      context.fn = this
      if (!args) {
        context.fn()
      } else {
        result = context.fn(...args)
        delete context.fn
      }
      return result
    }

    obj.introduce._es6apply(obj2, ['Evan You', 'china']) // hello, everyone, i am Evan You i am from china


http://www.niftyadmin.cn/n/566177.html

相关文章

MinIO NoClassDefFoundError: io/minio/MinioClient问题解决

问题描述&#xff1a; Caused by: java.lang.NoClassDefFoundError: io/minio/MinioClient 问题分析&#xff1a; 1、没有下载minio依赖导致报错。 解决办法&#xff1a; <dependency><groupId>io.minio</groupId><artifactId>minio</artifact…

MySQL 2059 - Authentication plugin ‘caching_sha2_password‘ cannot be loaded:问题解决

问题描述&#xff1a; 2059 - Authentication plugin caching_sha2_password cannot be loaded: 问题分析&#xff1a; 1、MySQL8.0.19数据库的当前登录用户加密规则为caching_sha2_password&#xff0c;使用Navicat12.0连不上。 解决办法&#xff1a; &#xff08;1&#…

js高级进阶——手动实现深拷贝函数

主要考察目标&#xff1a; 数据类型校验循环引用function deepClone(data, hash new WeakMap) {if (data undefined) return data;if (typeof data ! object) return data;if (data instanceof RegExp) return new RegExp(data)if (data instanceof Date) return new Date(d…

Java HTTP Status 400 – Bad Request问题解决

问题描述&#xff1a; HTTP Status 400 – Bad Request 问题分析&#xff1a; 1、参数里面带有特殊字符&#xff0c;比如|&#xff0c;特殊字符没有转义导致报错。 解决办法&#xff1a; |转义为%7C 。 2、Controller层的接口指定为Get方式请求&#xff0c;但是加上了Req…

观察者模式 vs 发布订阅模式

有一回面试&#xff0c;面试官问&#xff1a; 观察者模式&#xff0c;和发布订阅模式&#xff0c;有什么区别&#xff1f;我脑海中立刻闪现了《Head First设计模式》里讲的&#xff1a; Publishers Subscribers Observer Pattern“哼哼&#xff0c;我知道答案了&#xff0c;兄…

Spring Boot 实现后端进行参数校验

1 pom.xml(Maven依赖文件) <!--第一种方式导入校验依赖--><dependency><groupId>javax.validation</groupId><artifactId>validation-api</artifactId><version>2.0.1.Final</version></dependency><!--第二种方式导…

Vuex文档通读感悟

Vuex文档通读感悟 1、为什么要用 Vuex 进行状态管理&#xff1f;定义一个全局对象&#xff0c;再去上层封装了一些数据存取的接口不也可以么&#xff1f; Vuex 的状态存储是响应式的&#xff0c;当 Store 中状态发生改变时&#xff0c;对应的组件的视图能够得到高效的更新。你…

Java HttpMediaTypeNotSupportedException:Content type ‘xxx‘ not supported问题解决

问题描述&#xff1a; org.springframework.web.HttpMediaTypeNotSupportedException: Content type text/plain;charsetUTF-8 not supported 问题分析&#xff1a; 1、使用PostMan调试后端接口时&#xff0c;请求体里面的content type为text/plain&#xff0c;但是后端接口…