惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Last Watchdog
The Last Watchdog
博客园_首页
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
美团技术团队
小众软件
小众软件
V
V2EX
博客园 - Franky
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
The GitHub Blog
The GitHub Blog
Microsoft Security Blog
Microsoft Security Blog
Attack and Defense Labs
Attack and Defense Labs
S
Security Affairs
Simon Willison's Weblog
Simon Willison's Weblog
I
Intezer
T
The Exploit Database - CXSecurity.com
有赞技术团队
有赞技术团队
S
Schneier on Security
人人都是产品经理
人人都是产品经理
Security Archives - TechRepublic
Security Archives - TechRepublic
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
K
Kaspersky official blog
PCI Perspectives
PCI Perspectives
AI
AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
罗磊的独立博客
O
OpenAI News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The Register - Security
The Register - Security
V
Vulnerabilities – Threatpost
GbyAI
GbyAI
博客园 - 【当耐特】
C
Cisco Blogs
大猫的无限游戏
大猫的无限游戏
Help Net Security
Help Net Security
Google DeepMind News
Google DeepMind News
S
Securelist
Application and Cybersecurity Blog
Application and Cybersecurity Blog
P
Proofpoint News Feed
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
L
LangChain Blog
SecWiki News
SecWiki News
博客园 - 叶小钗
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V2EX - 技术
V2EX - 技术
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
L
LINUX DO - 热门话题
Cisco Talos Blog
Cisco Talos Blog

博客园 - 前端架构师

Claude Code 源码架构分析 我不是狐狸,我是那Harness Engineering 小龙虾的自我养成之路 vue3+ts添加公共富文本组件 vue watch详解 微信授权没搞过? css3图片防止变形 公众号使用微信sdk的正确姿势 web离线应用前提之离线检测 同学给我来个手写的new吧 this综合篇 this绑定番外篇之箭头函数绑定 vue中引入公用过滤器? this详解下 012天this详解上 011天之跨域资源共享CORS 010天JSON.stringify()详解 009天之跨浏览器的事件处理程序 使用XHR上传文件要不要了解一下?
es6手写一个call
前端架构师 · 2020-03-22 · via 博客园 - 前端架构师
Function.prototype.selfCall = function(ctx, ...args) {
    if(typeof this !== 'function') {
        throw new Error('you must use call with function')
    }
    ctx = ctx || window
    //强绑定上ctx为执行上下为
    ctx.qdleader = this
    const res = ctx.qdleader(...args);
    //删除这个,防止影响外部对象的正常运行
    delete ctx.qdleader
    return res;
}

还有种es6写法

Function.prototype.call3 = function(context) {
    context = context ? Object(context) : window;
    var fn = Symbol();
    context[fn] = this;
    
    let args = [...arguments].slice(1);
    let result = context[fn](...args);
    
    delete context[fn];
    return result;
}

那不用es6时候是咋写的呢?

Function.prototype.call2 = function(context) {
    //若传入context是null或者undefined时指向window
    //若传入的是原始数据类型,原生的call会调用Object()转换
    context = context ? Object(context):window;
    //创建一个独一无二的fn函数的命名
    var fn = fnFactor(context)
    
    //这里的this就是指调用call的那个函数
    //将调用的这个函数赋值到context种,这样之后执行context.fn的时候,fn里的this就是指向context了
    
    context[fn] = this;
    
    //定义一个用于放arguments的每一项的的字符串:['arguments[1]','arguments[2]']
    var args = [];
    //要从第一项开始,第0项是context
    
    for(var i = 1; i < arguemts.length; i ++) {
        args.push("arguments["+ i +"]")
    }
    //使用eval()来执行fn并将args一个个传递进去
    var result = eval("context[fn]("+ args +")")
    delete context[fn];
    
    return result;
}

加群一起进步