//实现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的更多相关文章

  1. 依据ECMA规范,手写一个bind函数

    Function.prototype.bind 函数,参见ECMA规范地址 如题,这次来实现一个boundFunction函数,不挂载在Function.prototype上,而是一个单独声明的函数. ...

  2. 手写简单call,apply,bind

    分析一下call的使用方法:call是显示绑定this指向,然后第一个参数是你所指向的this对象,后面跟着多个参数,以逗号隔开 function sum(num1,num2){ return num ...

  3. 手写call、apply、bind

    区别&联系 三者都是指定函数执行时的上下文,第一个参数都是上下文: call从第二个参数开始,后续所有的参数传递给函数执行: apply第二个参数是一个数组,传递给函数执行: bind返回一个 ...

  4. 手写Function.bind函数

    if(!Function.prototype.bind){ Function.prototype.bind = function(oThis){ if(typeof this !=="fun ...

  5. 手写一个bind

    1 Function.prototype.bind1 = function(){ 2 // 将类数组转化成数组 3 let arr = Array.prototype.slice.call(argum ...

  6. 手写系列:call、apply、bind、函数柯里化

    少废话,show my code call 原理都在注释里了 // 不覆盖原生call方法,起个别名叫myCall,接收this上下文context和参数params Function.prototy ...

  7. 前端面试手写代码——call、apply、bind

    1 call.apply.bind 用法及对比 1.1 Function.prototype 三者都是Function原型上的方法,所有函数都能调用它们 Function.prototype.call ...

  8. 源码来袭:bind手写实现

    JavaScript中的this指向规则 源码来袭:call.apply手写实现与应用 理解建议:如果对this指向规则不了解的话,建议先了解this指向规则,最好还能对call和apply的使用和内 ...

  9. 源码来袭:call、apply手写实现与应用

    关于this指向可以了解我的另一篇博客:JavaScript中的this指向规则. 一.call与apply的使用 回顾call与apply的this指向: var value = "win ...

  10. 手写AngularJS脏检查机制

    什么是脏检查 View -> Model 浏览器提供有User Event触发事件的API,例如,click,change等 Model -> View 浏览器没有数据监测API. Ang ...

随机推荐

  1. TZOJ 4024 游戏人生之梦幻西游(连续子段和绝对值最小)

    塔神酷爱玩梦幻西游这款游戏,这款游戏以著名的章回小说<西游记>故事为背景,透过Q版的人物,营造出浪漫的网络游戏风格.塔神以追求天下无敌为目标,从一个默默无闻的菜鸟,打拼到了登峰造极的大师, ...

  2. TZOJ 2965 A Coin Game(DP)

    描述 Farmer John's cows like to play coin games so FJ has invented with a new two-player coin game cal ...

  3. LA3516 Exploring Pyramids

    Exploring Pyramids 题目大意:给定一个欧拉序列(即每经过一个点,把这个点加入序列),问有多少种对应的多叉树 序列与树构造对应问题,考虑区间DP dp[i][j]表示序列i...j对应 ...

  4. Sublime keymap 个性修改

    [ // 保存全部 {"keys": ["ctrl+alt+s"], "command": "save_all"}, / ...

  5. web端的兼容性测试

    目前主流的浏览器有:chrome.firefox.safari.IE edge.Opera等.其中IE edge ,Google浏览器 和firefox被称为现代浏览器. 浏览器排行榜2019年4月浏 ...

  6. angular4 自定义表单组件

    自定义表单组件分为单值组件和多值组件. 单值组件:input/select/radio/textarea 多值组件:checkbox/tree组件 条件: 1.必须实现ControlValueAcce ...

  7. Angular js 具体应用(一)

    1,首先引用Angular  百度静态资源库搜索Angular  复制链接,在HTML中嵌入script 最好写在正文下面 <script type="text/javascript& ...

  8. 根据一个分类id 获取这个分类底下所有子分类的商品信息,根据下面方法查询出所有有关分类id 再 根据这些id去商品表里查询所有商品信息

    /** * 检测该分类下所有子分类,并输出ID(包括自己) * 数据库字段 catid pid */ function getChildrenIds ($sort_id){ include_once ...

  9. 灵动微本土MCU厂商具有吸引力的增长点

    作为各种电子产品的控制和处理核心,微控制单元(MCU)器件是一种集成微处理器(CPU).存储器(RAM/ROM).计数器,以及I/O端口的芯片.从MCU内核架构来看,单片机有历经多年的8051,基于A ...

  10. SQL优化神器 - Tosska SQL Tuning Expert Pro for Oracle

    SQL Tuning Expert Pro for Oracle 是Tosska 公司推出的划时代SQL优化工具.它可以帮助SQL开发人员和DBA: 找到最快的等价SQL: 调整执行计划: 管理SQL ...