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 = this
var 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、typeof 2、instanceof var a=[1,2];console.log(a instanceof Array) //true
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))
call、apply、bind的区别,模拟call、apply和bind的实现的更多相关文章
- 改变this指针的apply,call,bind的区别
apply.call 在 javascript 中,call 和 apply 都是为了改变某个函数运行时的上下文(context)而存在的,换句话说,就是为了改变函数体内部 this 的指向. Jav ...
- javascript中apply、call和bind的区别
在JS中,这三者都是用来改变函数的this对象的指向的,他们有什么样的区别呢.在说区别之前还是先总结一下三者的相似之处:1.都是用来改变函数的this对象的指向的.2.第一个参数都是this要指向的对 ...
- javascript中apply、call和bind的区别,容量理解,值得转!
a) javascript中apply.call和bind的区别:http://www.cnblogs.com/cosiray/p/4512969.html b) 深入浅出 妙用Javascrip ...
- apply,call,bind的区别
apply.call 在 javascript 中,call 和 apply 都是为了改变某个函数运行时的上下文(context)而存在的,换句话说,就是为了改变函数体内部 this 的指向. Jav ...
- JS 中的this指向问题和call、apply、bind的区别
this的指向问题 一般情况下this对象指向调用函数的对象,全局环境中执行函数this对象指向window. function a(){ console.log(this); //输出函数a中的th ...
- javascript中apply、call和bind的区别及方法详解
文章目录 apply.call apply.call 区别 apply.call实例 数组之间追加 获取数组中的最大值和最小值 验证是否是数组(前提是toString()方法没有被重写过) 类(伪 ...
- apply,all,bind的区别
这三个都是用来改变this指向的 call() 和apply()的第一个参数相同,就是指定的对象.这个对象就是该函数的执行上下文.call()和apply()的区别就在于,两者接收的参数不一样.cal ...
- call(),apply()和bind()的区别
javascript中的每一个Function对象都有一个apply()和一个call()方法,它们的语法分别是: /*apply()方法*/ function.apply(thisObj[, arg ...
- js中call、apply和bind的区别
在JS中,这三者都是用来改变函数的this对象的指向的,他们有什么样的区别呢.在说区别之前还是先总结一下三者的相似之处:1.都是用来改变函数的this对象的指向的.2.第一个参数都是this要指向的对 ...
- call apply bind 的区别
1.call和apply都是对函数的直接调用,而bind方法返回的仍然是一个函数,因此后面还需要()来进行调用才可以 var xw={ name: "小王", gender: &q ...
随机推荐
- (生鲜项目)05. RESTful api, 和 VUE
第一步: 什么是 RESTful api 总结: 使用http协议作为介质, 达到客户端修改服务器端资源的目的, 服务器只需要提供指定的api接口, 客户端根据http协议中的post/get/put ...
- LeetCode 731. My Calendar II
原题链接在这里:https://leetcode.com/problems/my-calendar-ii/ 题目: Implement a MyCalendarTwo class to store y ...
- maven install
1. install maven under ubuntu apt install maven 2 speed up package download vim ~/.m2/settings.xml & ...
- circus 架构
转自官方文档:https://circus.readthedocs.io/en/latest/design/architecture/ Overall architecture Circus is c ...
- 开发框架Express
一.使用原因 由于nodejs原生的http核心模块在某些方面不足以应对开发需求,所以就需要使用框架来加快开发效率,让代码更高度统一.在nodejs中有许多web开发框架,以下介绍Express的使用 ...
- 回归模型的性能评价指标(Regression Model Performance Evaluation Metric)
回归模型的性能评价指标(Performance Evaluation Metric)通常有: 1. 平均绝对误差(Mean Absolute Error, MAE):真实目标y与估计值y-hat之间差 ...
- 缺陷的严重程度(Severity)
该bug对用户造成的影响有多大.(1)Urgent 死机重启等致命bug(2)Veryhigh 非常严重的bug(3)High 严重的bug(4)Medium 中等程度的bug(5)Low 小的bug
- Alpha冲刺(4/6)
队名:無駄無駄 组长博客 作业博客 组员情况 张越洋 过去两天完成了哪些任务 摸鱼 提交记录(全组共用) 接下来的计划 沟通前后端成员,监督.提醒他们尽快完成各自的进度 学习如何评估代码质量 准备Al ...
- linux性能监控常用命令
概述 我们在linux下,如果想要监控服务器性能.我们必须掌握以下常用的指标查看命令. ps pstree top free vmstat sar ps ps命令能给出当前系统中进程的快照.下面我们列 ...
- 273道题目;更新到java题目里面 (已迁移到其他类目下面,存储)
1. Java 基础 1.JDK 和 JRE 有什么区别? 2. == 和 equals 的区别是什么? 3. 两个对象的 hashCode() 相同,则 equals() 也一定为 true,对吗? ...