手写call,bind,apply
- //实现call
- var that = this ; //小程序环境
- function mySymbol(obj){
- let unique = (Math.random() + new Date().getTime()).toString(32).slice(0,8);
- if(obj.hasOwnProperty(unique)){
- return mySymbol(obj)
- }else {
- return unique;
- }
- }
- let Person = {
- name:'Tom',
- say(age = 23,city = '宁波',job = '前端'){
- console.log(`我叫${this.name},今年${age},在${city},从事${job}`)
- }
- }
- let Person1 = {
- name:'Tom1'
- }
- Function.prototype.MyCall = function(context){
- context =context || that; //小程序环境 PC环境为context =context || window;
- let fn = mySymbol(context);
- context.fn = this;
- let arg = [...arguments].slice(1);
- context.fn(...arg);
- delete context.fn;
- }
- Person.say.MyCall(Person1,23,'宁波','前端');
- Person.say.call(Person1,23,'宁波','前端');
- //实现apply
- Function.prototype.myApply = function(cxt,args){
- cxt = cxt||that; //小程序环境为cxt = cxt||that; PC环境为cxt = cxt||window;
- var fn = mySymbol(cxt);
- cxt[fn] = this;
- var result = cxt[fn](...args);
- return result;
- }
- Person.say.myApply(Person1,['23','宁波','前端'])
- Person.say.apply(Person1,['23','宁波','前端'])
- Function.prototype.myBind = function(context){
- let self = this;
- let arg = [...arguments].slice(1);
- return function(){
- let newArg = [...arguments];
- return self.apply(context,arg.concat(newArg))
- }
- }
- let fn = Person.say.myBind(Person1,23);
- fn();
- fn('宁波','前端')
记录学习
手写call,bind,apply的更多相关文章
- 依据ECMA规范,手写一个bind函数
Function.prototype.bind 函数,参见ECMA规范地址 如题,这次来实现一个boundFunction函数,不挂载在Function.prototype上,而是一个单独声明的函数. ...
- 手写简单call,apply,bind
分析一下call的使用方法:call是显示绑定this指向,然后第一个参数是你所指向的this对象,后面跟着多个参数,以逗号隔开 function sum(num1,num2){ return num ...
- 手写call、apply、bind
区别&联系 三者都是指定函数执行时的上下文,第一个参数都是上下文: call从第二个参数开始,后续所有的参数传递给函数执行: apply第二个参数是一个数组,传递给函数执行: bind返回一个 ...
- 手写Function.bind函数
if(!Function.prototype.bind){ Function.prototype.bind = function(oThis){ if(typeof this !=="fun ...
- 手写一个bind
1 Function.prototype.bind1 = function(){ 2 // 将类数组转化成数组 3 let arr = Array.prototype.slice.call(argum ...
- 手写系列:call、apply、bind、函数柯里化
少废话,show my code call 原理都在注释里了 // 不覆盖原生call方法,起个别名叫myCall,接收this上下文context和参数params Function.prototy ...
- 前端面试手写代码——call、apply、bind
1 call.apply.bind 用法及对比 1.1 Function.prototype 三者都是Function原型上的方法,所有函数都能调用它们 Function.prototype.call ...
- 源码来袭:bind手写实现
JavaScript中的this指向规则 源码来袭:call.apply手写实现与应用 理解建议:如果对this指向规则不了解的话,建议先了解this指向规则,最好还能对call和apply的使用和内 ...
- 源码来袭:call、apply手写实现与应用
关于this指向可以了解我的另一篇博客:JavaScript中的this指向规则. 一.call与apply的使用 回顾call与apply的this指向: var value = "win ...
- 手写AngularJS脏检查机制
什么是脏检查 View -> Model 浏览器提供有User Event触发事件的API,例如,click,change等 Model -> View 浏览器没有数据监测API. Ang ...
随机推荐
- ubuntu下搜狗输入法待选框中文乱码问题解决(重启搜狗输入法)
简单的三个命令就可以解决 killall fcitx //关闭fcitx killall sogou-qimpanel //关闭搜狗输入法 fcitx //打开fcitx
- H5C3--边框图片
类似于android的.9图片,目的是为了防止图片因为内容的扩展而导致图片拉伸失真. <!DOCTYPE html> <html lang="en"> &l ...
- @import vs #import - iOS 7
It's a new feature called Modules or "semantic import". There's more info in the WWDC 2013 ...
- vim 简明教程(转自飘过的小牛)
vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...
- Eclipse安装Spket插件
Eclipse安装Spket插件 1.在线安装,地址:Spket - http://www.agpad.com/update 2.下载插件包安装, 地址:http://www.spket.com/
- python_数据类型_list
names = ['one','two','three','four','five'] #列表切片 print(names[0:]) #['one', 'two', 'three', 'four', ...
- springmvc配置不拦截静态资源
<mvc:resources mapping="/js/**" location="/js/"/>
- python OneHot编码
- 洛谷P4145 上帝造题的七分钟2 / 花神游历各国(重题:洛谷SP2713 GSS4 - Can you answer these queries IV)
题目背景 XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. 题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟,L说,要能修改,于是便有了对一段 ...
- JavaScript模式:字面量和构造函数
本篇主要讨论了通过字面量以构造对象的方法,比如对象.数组以及正则表达式等字面量的构造方法,同时还讨论了与类似Object()和Array()等内置构造函数相比,为什么基于字面量表示法是更为可取. 对象 ...