1. //实现call
  2. var that = this ; //小程序环境
  3. function mySymbol(obj){
  4. let unique = (Math.random() + new Date().getTime()).toString(32).slice(0,8);
  5. if(obj.hasOwnProperty(unique)){
  6. return mySymbol(obj)
  7. }else {
  8. return unique;
  9. }
  10. }
  11. let Person = {
  12. name:'Tom',
  13. say(age = 23,city = '宁波',job = '前端'){
  14. console.log(`我叫${this.name},今年${age},在${city},从事${job}`)
  15. }
  16. }
  17. let Person1 = {
  18. name:'Tom1'
  19. }
  20. Function.prototype.MyCall = function(context){
  21. context =context || that; //小程序环境 PC环境为context =context || window;
  22. let fn = mySymbol(context);
  23. context.fn = this;
  24. let arg = [...arguments].slice(1);
  25. context.fn(...arg);
  26. delete context.fn;
  27. }
  28. Person.say.MyCall(Person1,23,'宁波','前端');
  29. Person.say.call(Person1,23,'宁波','前端');
  30. //实现apply
  31.  
  32. Function.prototype.myApply = function(cxt,args){
  33. cxt = cxt||that; //小程序环境为cxt = cxt||that; PC环境为cxt = cxt||window;
  34. var fn = mySymbol(cxt);
  35. cxt[fn] = this;
  36. var result = cxt[fn](...args);
  37. return result;
  38. }
  39. Person.say.myApply(Person1,['23','宁波','前端'])
  40. Person.say.apply(Person1,['23','宁波','前端'])
  41.  
  42. Function.prototype.myBind = function(context){
  43. let self = this;
  44. let arg = [...arguments].slice(1);
  45. return function(){
  46. let newArg = [...arguments];
  47. return self.apply(context,arg.concat(newArg))
  48. }
  49. }
  50.  
  51. let fn = Person.say.myBind(Person1,23);
  52. fn();
  53. 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. ubuntu下搜狗输入法待选框中文乱码问题解决(重启搜狗输入法)

    简单的三个命令就可以解决 killall fcitx //关闭fcitx killall sogou-qimpanel //关闭搜狗输入法 fcitx //打开fcitx

  2. H5C3--边框图片

    类似于android的.9图片,目的是为了防止图片因为内容的扩展而导致图片拉伸失真. <!DOCTYPE html> <html lang="en"> &l ...

  3. @import vs #import - iOS 7

    It's a new feature called Modules or "semantic import". There's more info in the WWDC 2013 ...

  4. vim 简明教程(转自飘过的小牛)

    vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...

  5. Eclipse安装Spket插件

    Eclipse安装Spket插件 1.在线安装,地址:Spket - http://www.agpad.com/update 2.下载插件包安装, 地址:http://www.spket.com/

  6. python_数据类型_list

    names = ['one','two','three','four','five'] #列表切片 print(names[0:]) #['one', 'two', 'three', 'four', ...

  7. springmvc配置不拦截静态资源

    <mvc:resources mapping="/js/**" location="/js/"/>

  8. python OneHot编码

  9. 洛谷P4145 上帝造题的七分钟2 / 花神游历各国(重题:洛谷SP2713 GSS4 - Can you answer these queries IV)

    题目背景 XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. 题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟,L说,要能修改,于是便有了对一段 ...

  10. JavaScript模式:字面量和构造函数

    本篇主要讨论了通过字面量以构造对象的方法,比如对象.数组以及正则表达式等字面量的构造方法,同时还讨论了与类似Object()和Array()等内置构造函数相比,为什么基于字面量表示法是更为可取. 对象 ...