区别&联系

三者都是指定函数执行时的上下文,第一个参数都是上下文;

call从第二个参数开始,后续所有的参数传递给函数执行;

apply第二个参数是一个数组,传递给函数执行;

bind返回一个指定上下文的方法,等待后续执行。

实现:

Function.prototype._call = function(ctx = window, ...args) {
ctx.fn = this
const result = ctx.fn(...args)
delete ctx.fn
return result
} Function.prototype._apply = function(ctx = window, args) {
ctx.fn = this
const result = ctx.fn(...args)
delete ctx.fn
return result
} Function.prototype._bind = function(ctx = window) {
const _this = this
ctx.fn = this
return function F(...args2) {
// 判断是否用于构造函数
if (this instanceof F) {
return new _this(...args1, ...args2)
}
return _this.apply(context, args1.concat(args2))
}
} const fn = function(...args) {
console.log(this.name, args)
} const obj = {
name: 'obj'
} fn._call(obj, 1, 2, 3)
fn._apply(obj, [1, 2, 3])
fn._bind(obj)(1, 2, 3)

存在的问题

1.如果传入的ctx(上下文环境)不是Object类型,则需要将其替换成对应的包装类型;

2.直接给传入的ctx增加一个fn,然后用delete去删除是否不太好?

手写call、apply、bind的更多相关文章

  1. JS手写call、bind、apply

    call方法的实现 Function.prototype.MyCall = function(content,...args){ const self = content || window; con ...

  2. 手写call,bind,apply

    //实现call var that = this ; //小程序环境 function mySymbol(obj){ let unique = (Math.random() + new Date(). ...

  3. 手写call,apply方法实现

    call Function.prototype.myCall = function(){ var object = arguments[0]; var arr = []; for(var i = 1; ...

  4. 前端面试 js 你有多了解call,apply,bind?

    函数原型链中的 apply,call 和 bind 方法是 JavaScript 中相当重要的概念,与 this 关键字密切相关,相当一部分人对它们的理解还是比较浅显,所谓js基础扎实,绕不开这些基础 ...

  5. 手写系列:call、apply、bind、函数柯里化

    少废话,show my code call 原理都在注释里了 // 不覆盖原生call方法,起个别名叫myCall,接收this上下文context和参数params Function.prototy ...

  6. 前端面试手写代码——call、apply、bind

    1 call.apply.bind 用法及对比 1.1 Function.prototype 三者都是Function原型上的方法,所有函数都能调用它们 Function.prototype.call ...

  7. 手写简单call,apply,bind

    分析一下call的使用方法:call是显示绑定this指向,然后第一个参数是你所指向的this对象,后面跟着多个参数,以逗号隔开 function sum(num1,num2){ return num ...

  8. 源码来袭:bind手写实现

    JavaScript中的this指向规则 源码来袭:call.apply手写实现与应用 理解建议:如果对this指向规则不了解的话,建议先了解this指向规则,最好还能对call和apply的使用和内 ...

  9. 源码来袭:call、apply手写实现与应用

    关于this指向可以了解我的另一篇博客:JavaScript中的this指向规则. 一.call与apply的使用 回顾call与apply的this指向: var value = "win ...

  10. 依据ECMA规范,手写一个bind函数

    Function.prototype.bind 函数,参见ECMA规范地址 如题,这次来实现一个boundFunction函数,不挂载在Function.prototype上,而是一个单独声明的函数. ...

随机推荐

  1. uboot中Kconfig架构的理解

    1./u-boot-2019.07/Kconfig 是顶层Kconfig mainmenu "U-Boot $UBOOTVERSION Configuration"  #这是总me ...

  2. java.util.Date 与 java.sql.Date 相关知识点解析

    问:java.sql.Date 和 java.util.Date 有什么区别?   答:这两个类的区别是 java.sql.Date是针对 SQL 语句使用的,它只包含日期而没有时间部分,一般在读写数 ...

  3. Java缓冲字符读取

    public class BufferedReaderDemo { public static void main(String[] args) throws IOException { // 创建流 ...

  4. luogu P4103 [HEOI2014]大工程 虚树 + 树形 DP

    Description 国家有一个大工程,要给一个非常大的交通网络里建一些新的通道.  我们这个国家位置非常特殊,可以看成是一个单位边权的树,城市位于顶点上.  在 2 个国家 a,b 之间建一条新通 ...

  5. 大数据分析:hadoop工具

    一.hadoop工具 Hadoop介绍: Hadoop是一个由Apache基金会所开发的分布式系统基础架构.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速运算和存储 ...

  6. Spring Security 3.1 中功能强大的加密工具 PasswordEncoder

    Spring Security 3.1 中功能强大的加密工具 PasswordEncoder 博客分类: security spring springsecurity  好吧,这种加密机制很复杂,还是 ...

  7. vue2.0 之 douban (六)axios的简单使用

    由于项目中用到了豆瓣api,涉及到跨域访问,就需要在config的index.js添加代理,例如 proxyTable: { // 设置代理,解决跨域问题 '/api': { target: 'htt ...

  8. __I、 __O 、__IO是什么意思?怎么用?

    出处:http://www.chuxue123.com/forum.php?mod=viewthread&tid=122&ctid=3 __I. __O .__IO是什么意思?这是ST ...

  9. PHP-图片处理

    开启 GD 扩展(php_gd2.dll) 创建画布 画布:一种资源型数据,可以操作的图像资源. 创建新画布(新建) ImageCreate(宽,高); 创建基于调色板的画布. imageCreate ...

  10. ElasticSearch删除索引

    curl -X DELETE http://{ES IP address}:9200/{index_name}