js new call apply bind 的 原理
new
new 做了什么事?
1. 以 Object.protoype 为原型创建一个新对象
2. 以新对象为 this,执行函数的 [[call]]
3. 如果 [[call]] 的返回值是对象,那么,返回这个对象,否则返回第一步创建的新对象
function myNew(fn, ...args) {
const obj = Object.create(fn.prototype);
const ret = fn.call(obj, ...args);
return ret instanceof Object ? ret : obj;
}
call
实际上就是把方法挂在对象上,执行然后删除
Function.prototype.myCall = function(context, ...args) {
if (typeof this !== 'function') {
throw new TypeError('this is not a function')
}
context = context || window;
context.fn = this;
const ret = context.fn(...args);
delete context.fn;
return ret
}
apply
原理和call一样
Function.prototype.myApply = function (context, arg) {
if (typeof this !== 'function') {
throw new TypeError('this is not a function')
}
context = context || window;
context.fn = this;
let ret;
if (arg) {
ret = context.fn(...args);
} else {
ret = context.fn();
}
delete context.fn;
return ret
}
bind
bind原理就是封一层闭包
function.prototype.myBind = function (context, ...bindArgs) {
if (typeof this !== 'function') {
throw new TypeError('this is not a function');
}
const _this = this;
return function Fn(...execArgs) {
const args = bindArgs.concat(execArgs);
return _this.call(this instanceof Fn ? this : context, ...args);
}
}
js new call apply bind 的 原理的更多相关文章
- JS 的 call apply bind 方法
js的call apply bind 方法都很常见,目的都是为了改变某个方法的执行环境(context) call call([thisObj[,arg1[, arg2[, [,.argN]]]] ...
- JS中call,apply,bind方法的总结
why?call,apply,bind干什么的?为什么要学这个? 一般用来指定this的环境,在没有学之前,通常会有这些问题. var a = { user: "小马扎", fn: ...
- 学习前端的菜鸡对JS的call,apply,bind的通俗易懂理解
call,apply,bind 在JavaScript中,call.apply和bind是Function对象自带的三个方法,都是为了改变函数体内部 this 的指向. a ...
- 原生JS实现call,apply,bind函数
1. 前言 使用原生JS实现call和apply函数,充分了解其内部原理.call和apply都是为了解决改变this的指向.作用都相同,只是传参的方式不同.除了第一个参数外,call可以接受一个参数 ...
- js: this,call,apply,bind 总结
对js中的一些基本的很重要的概念做一些总结,对进一步学习js很重. 一.this JavaScript 中的 this 总是指向一个对象,而具体指向那个对象是在运行时基于函数的执行环境动态绑定的,而非 ...
- js 中call,apply,bind的区别
call.apply.bind方法的共同点与区别: apply.call.bind 三者都是用来改变函数的this对象的指向: apply.call.bind 三者都可以利用后续参数传参: bind ...
- js笔记——call,apply,bind使用笔记
call和apply obj.call(thisObj, arg1, arg2, ...); obj.apply(thisObj, [arg1, arg2, ...]); 两者作用一致,都是把obj( ...
- JS之call/apply/bind
测试代码: var a = 1; var obj = { a = 2; } function test(a){ alert(a); alert(this.a); } 1.test(3); 结果:3,1 ...
- js 对call apply bind理解
请参考 http://www.cnblogs.com/xljzlw/p/3775162.html 1.call和apply的区别:参数类型不同var mtt = { name: "mtt&q ...
随机推荐
- UOJ220 [NOI2016] 网格 【割顶】【并查集】
题目分析: 答案显然只有{-1,0,1,2}四种. 对于答案等于-1的情况,只有两种情况,一种是只剩一只跳蚤,另一种是只剩两只跳蚤且他们四连通,这个很好判. 对于答案等于0的情况,那说明联通块大于1, ...
- 音视频入门-10-使用libyuv对YUV数据进行缩放、旋转、镜像、裁剪、混合
* 音视频入门文章目录 * libyuv libyuv 是 Google 开源的实现各种 YUV 与 RGB 之间相互转换.旋转.缩放等的库.它是跨平台的,可在 Windows.Linux.Mac.A ...
- App功能测试点总结
1.手机操作系统android(谷歌).ios(苹果).Windows phone(微软).Symbian(诺基亚).BlackBerry OS(黑莓).windows mobile(微软),目前主流 ...
- WCF header 域
[OperationContract] [WebInvoke(UriTemplate = "poststr1")] public string poststr1(csinfo cs ...
- LunHui 的生命观
LunHui 的生命观 来源 https://www.zhihu.com/question/346510295 作者:齐天大圣链接:https://www.zhihu.com/question/346 ...
- CentOS 6.x 配置iptables
CentOS 6.x 配置iptables 来源 https://www.cnblogs.com/chillax1314/p/7976067.html iptables -P INPUT DROP-- ...
- 远程 Linux(Ubuntu 18)添加字体
安装 xshell与xftp 连接xshell 点击 xshell上方工具栏中的xftp图标, 自动连接xftp linux下创建字体目录 su cd / cd usr/share/fonts mkd ...
- vue 鼠标移入移出事件执行多次(尤其ie)
来自:https://www.cnblogs.com/myfirstboke/p/9150809.html 侵删 <p @mouseover="over($event)" ...
- js数组实现上移下移
up(index) { if(index === 0) { return } //在上一项插入该项 this.list.splice(index - 1, 0, (this.list[index])) ...
- jupyter修改默认目录
有趣的事,Python永远不会缺席! 如需转发,请注明出处:小婷儿的python https://www.cnblogs.com/xxtalhr/p/10841241.html 一.修改 win10 ...