手写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 ...
随机推荐
- win8 装的SQL Server2012 企业版
链接:ed2k://|file|cn_sql_server_2012_enterprise_edition_x86_x64_dvd_813295.iso|5054384128|BC78EFDC4005 ...
- 读书笔记--Head First JavaScript 目录
1.交互式网络 2.存储数据 3.探索客户端 4.决策 5.循环 6.函数 7.表单与验证 8.驾驭网页 9.为数据带来生命 10.创建自定义对象 11.除错务尽 12.动态数据
- 使用Jedis操作Redis-使用Java语言在客户端操作---hash类型
我们可以将Redis中的Hashes类型看成具有String Key和String Value的map容器. 所以该类型非常适合于存储值对象的信息.如Username.P ...
- eclipse配置mybatis xml文件自动提示
如果使用eclipse中,再写mybatis的xml文件的时候,没有提示,用“Alt+/”,不能把代码用快捷键敲出来,通过下面这个方法,可以解决. 1.下载一个文件,找一个专门的地方保存,配置自动提示 ...
- 通过button将form表单的数据提交到action层
form表单中不需要写action的路径,需要给form表单一个唯一的id,将你要提交的信息的表单中的标签name="action中的javabean对象.javabean属性". ...
- day37 09-Struts2和Hibernate整合环境搭建
<!-- 设置本地Session --> <property name="hibernate.current_session_context_class"> ...
- Vbulletin Used to Show Malicious Advertisements
In the past, we have seen a massive amount of vBulletin websites compromised through theVBSeo Vulner ...
- callee和caller属性的区别
在函数内部,有两个特殊的对象:arguments和this .arguments是一个类数组对象,用于存放传入函数中的所有参数. callee是arguments对象的属性,caller是所有函数对象 ...
- Hdu 4251 区间中位数(划分树)
题目链接 The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 32768/3276 ...
- java jsp j2ee
1. JavaScript用于编写嵌入在网页文档中的程序,它由浏览器负责解释和执行,可以在网页上产生动态的显示效果和实现与用户交互的功能,譬如,让一串文字跟着鼠标移动,让一个图标在网页漂浮移动,验证用 ...