call、apply、bind的区别,模拟call、apply和bind的实现
不传参的方法:
Function.prototype.bind2 = function (context) { var self = this; return function () { return self.call(context); } }
传参的方法:
Function.prototype.myBind = function (objCtx) { if (typeof this !== 'function') { throw new TypeError('Error'); } let ctx = objCtx || window; let _this = this; let args = [...arguments].slice(1); let Fbind = function () { let self = this instanceof Fbind ? this : ctx; return _this.apply(self, args.concat(...arguments)); // 这里可以使用 call 方法 // let bindArgs = args.concat(...arguments); // return _this.call(self, ...bindArgs); } let f = function () {}; f.prototype = this.prototype; Fbind.prototype = new f(); return Fbind; }
以上就是 call
的思路,apply
的实现也类似
不传参的方法:
Function.prototype.bind2 = function (context) { var self = this; return function () { return self.apply(context); } }
传参的方法:
Function.prototype.myBind = function (objCtx) { if (typeof this !== 'function') { throw new TypeError('Error'); } let ctx = objCtx || window; let _this = this; let args = [...arguments].slice(1); let Fbind = function () { let self = this instanceof Fbind ? this : ctx; return _this.apply(self, args.concat(...arguments)); // 这里可以使用 call 方法 // let bindArgs = args.concat(...arguments); // return _this.apply(self, ...bindArgs); } let f = function () {}; f.prototype = this.prototype; Fbind.prototype = new f(); return Fbind; }
bind
和其他两个方法作用也是一致的,只是该方法会返回一个函数。并且我们可以通过 bind
实现柯里化。
同样的,也来模拟实现下 bind
Function.prototype.myBind = function (context) {if (typeof this !== 'function') {throw new TypeError('Error')}var _this = thisvar args = [...arguments].slice(1)// 返回一个函数return function F() {// 因为返回了一个函数,我们可以 new F(),所以需要判断if (this instanceof F) {return new _this(...args, ...arguments)}return _this.apply(context, args.concat(...arguments))} }
1、typeof2、instanceof 3、Object.prototype.toString.call()var arr = [10,20,03,04];var fn = function(){}var img = new Image()var d = new Date()console.log(arr.toString())console.log(Object.prototype.toString.call(d))
转载于:https://www.cnblogs.com/houjl/p/10086576.html
call、apply、bind的区别,模拟call、apply和bind的实现相关推荐
- call apply 和 bind的区别
apply 和call 在 javascript 中,call 和 apply 都是为了改变某个函数运行时的上下文(context)而存在的,换句话说,就是为了改变函数体内部 this 的指向. Ja ...
- call、apply 以及 bind 的区别和用法
call.apply.bind call和apply共同点 共同点:改变函数执行时的上下文,将一个对象的方法交给另一个对象来执行,并且是立即执行的 call和apply的对象,必须是一个函数Funct ...
- 不用call和apply方法模拟实现ES5的bind方法
本文首发我的个人博客:前端小密圈,评论交流送1024邀请码,嘿嘿嘿?. 来自朋友去某信用卡管家的做的一道面试题,用原生JavaScript模拟ES5的bind方法,不准用call和bind方法. 至于 ...
- 可能遇到假的面试题:不用call和apply方法模拟实现ES5的bind方法
本文首发我的个人博客:前端小密圈,评论交流送1024邀请码,嘿嘿嘿?. 来自朋友去某信用卡管家的做的一道面试题,用原生JavaScript模拟ES5的bind方法,不准用call和bind方法. 至于 ...
- javascript中apply、call和bind的区别
在JS中,这三者都是用来改变函数的this对象的指向的,他们有什么样的区别呢. 在说区别之前还是先总结一下三者的相似之处: 1.都是用来改变函数的this对象的指向的. 2.第一个参数都是this要指 ...
- call(),apply()和bind()的区别和应用以及扩展
首先三个方法的作用: 1:都是在特定的作用域中调用函数,等于设置函数体内this对象的值,以扩充函数赖以运行的作用域. 2:都是改变this的指向方向 区别: call()和apply()接受参数的方 ...
- call、apply、bind的区别?(面试题-JavaScript部分)
共同点: 可以改变this指向 语法: 函数.call().函数.apply().函数.bind() 不同点: 1. call.apply可以立即执行. bind不会立即执行,因为bind返回的是一个 ...
- 面试官问:能否模拟实现JS的bind方法(高频考点)
可以点击上方的话题JS基础系列,查看往期文章 写于2018年11月21日,发布在掘金阅读量1.3w+ 前言 这是面试官问系列的第二篇,旨在帮助读者提升JS基础知识,包含new.call.apply.t ...
- SQL 关于apply的两种形式cross apply 和 outer apply
SQL 关于apply的两种形式cross apply 和 outer apply 阅读目录 SQL 关于apply的两种形式cross apply 和 outer apply Sql学习第四天--S ...
- SQL 关于apply的两种形式cross apply 和 outer apply(转)
转载链接:http://www.cnblogs.com/shuangnet/archive/2013/04/02/2995798.html apply有两种形式: cross apply 和 oute ...
最新文章
- 代码实践 | CVPR2020——AdderNet(加法网络)迁移到检测网络(代码分享)
- iOS开发网络数据之AFNetworking使用
- 使用DotNetCharting控件生成报表统计图总结
- http,session,cookie
- 中国移动云能力中心 Iaas 产品部软件开发工程师陈焱山:社区始于连接,也成就于连接...
- MyEclipse2015修改项目部署名称(WebContextRoot)
- python图像处理模糊_Python+OpenCV图像处理之模糊操作
- 一般市区有测速吗_高速公路增加这么多龙门架,有抓拍超速违章功能吗?可要仔细辨别...
- 【iOS QR Code】集成ZXingWidget(XCode Version 4.5.2,iOS 6.0 SDK)
- sap 发送mesage_SAP系统和微信集成的系列教程之九:如何将微信用户发送给微信公众号的内容自动转存到SAP C4C系统...
- scrapy 快速入门
- gsonformat java代码_GSONFormat的简单使用
- 设备管理器---usb大容量储存设备感叹号,USB大容量存储设备无法启动
- ffmpeg生成缩略图
- frp 内网穿透 多系统 多模式(详细+多个应用)
- NAT与NAT穿透(一)
- 树莓派瞎折腾[1]-实现简单的命令行音乐播放器
- Verdi详细使用总结
- 网络安全及网络安全评估的脆弱性分析
- 中式红木装修——一个会呼吸的家