//实现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. 入门servlet:request获取请求体数据

    @WebServlet("/RequestDemo5") public class RequestDemo5 extends HttpServlet { protected voi ...

  2. zabbix告警模板

    邮件 webhook模板 ZABBIX告警通知 告警状态:[{TRIGGER.STATUS}] 告警主机:[{HOST.NAME}] 主机地址:[{HOST.IP}] 告警时间:[{EVENT.DAT ...

  3. 【python之路32】python异常处理

    一.捕获异常 1.try  except #!usr/bin/env python # -*- coding:utf-8 -*- num = input("请输入一个数字:") t ...

  4. 八大 IoT 安全关键技术解析

    IoT 设备的增长也伴随着网络攻击的风险的增长,因此在设计产品时就必须考虑到系统的安全. 高德纳咨询公司最近的报告预测,到 2020 年,全世界将有 200.4 亿的物联网设备相互连接,且平均每天约还 ...

  5. iFrame 父子窗口通讯

    今天就来说说 iFrame 的父子窗口通讯,关于 iFrame 这里就不陈述了,想要了解的盆友可以百度一下, 由于项目需要,前些天用到了个弹框框架 layer 弹出层,有很多弹出的方式,其中一种就是用 ...

  6. gomod

    package main import "mycore" func main(){ mycore.ShowName() } main.go module mytest requir ...

  7. 解决Cesium1.50对gltf2.0/3dtiles数据读取的问题

    问题说明 Cesium 1.50(2018/10/01)版本打开3dtiles可能会出现加载不上导致渲染停止的错误. 错误说明为:RuntimeError: Unsupported glTF Exte ...

  8. https方式nginx 代理tomcat访问不带www的域名301重定向跳转到www的域名帮助seo集中权重

    比如我要把 http://gucanhui.com http://www.gucanhui.com 跳转到https://www.gucanhui.com 用F12的network可以看到状态码301 ...

  9. hive-hbase表的建立

    HIVE-HBASE表CDH4的时候不成熟select*的时候会查询不出(必须通过字段名称),CDH5已经修复这点需要注意 接口表hive部分一般不做join select会占性能,因为hbase接口 ...

  10. 2019-10-18-WPF-解决-StylusPlugIn-点击穿透问题

    title author date CreateTime categories WPF 解决 StylusPlugIn 点击穿透问题 lindexi 2019-10-18 20:55:35 +0800 ...