手写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 ...
随机推荐
- [BZOJ2729]排队
数学知识 排列 A(n,m)从n个元素中选出m个的不同的排列数 A(n,m)=n!/(n-m)! 组合 C(n,m)从n个元素中选出m个的不同的方案数 C(n,m)=n!/(m!*(n-m)! ...
- [Array]448. Find All Numbers Disappeared in an Array
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...
- bzoj 3029 守卫者的挑战——概率期望dp+状态数思考
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3029 先随便写了个dfs,记录“前 i 次.成功 j 次.容量-残片=k”的概率.因为是否可 ...
- 前端小知识--区分get和post请求
get和post是HTTP协议中的两种发送请求的方法. 如果你还不了解http,可以点击[HTTP协议①介绍](https://www.jianshu.com/p/632b890b75ac)[HTTP ...
- 前端(Node.js)(1)-- 初识Node.js
1.认识 Node.js 诞生.发展.应用现状.生态圈等方面 1.1. 2008年 RyanDahl的目标是创建一个易扩展.适用于现代Web应用通信的服务器平台 1.2.国内外的应用情况 Linked ...
- 彻底删除 Git 项目中的文件(BFG Repo-Cleaner 用法)
一些时候由于开发初期经验不足和贪图方便, 会把一些不应该提交到 Git 的文件上传到 Github, 带来一系列安全问题, 更有可能是把一些大文件上传到 GitHub 上, 导致项目非常臃肿, 每次 ...
- git中的错误
! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'git@gitee.co ...
- Vbulletin Used to Show Malicious Advertisements
In the past, we have seen a massive amount of vBulletin websites compromised through theVBSeo Vulner ...
- java中的线程(2):如何正确停止线程之2种常见停止方式
1.常见停止方式 结束run函数,run中含退出标志位. 使用interrupt()方法中断线程 使用stop方法暴力终止(已经弃用) 2.结束run class TestThread extends ...
- Django用户登陆以及跳转后台管理页面2
请先写好以下,再来替换文件 Django用户登陆以及跳转后台管理页面1http://www.cnblogs.com/ujq3/p/7891774.html from django.shortcuts ...