首页 > 其他分享 >手写apply-call-bind实现

手写apply-call-bind实现

时间:2023-08-26 20:12:10浏览次数:41  
标签:function let bind argArray call result thisArg apply fn

call

 1 Function.prototype.myCall=function(thisArg,...args){
 2     let fn=this //隐式调用
 3     thisArg=(thisArg!==null&&thisArg!==undefined)?Object(thisArg):window //传undefined null 指向window全局
 4     thisArg.fn=fn //1
 5     let result=thisArg.fn(...args) //2
 6     delete thisArg.fn //3  1 2 3不用call调用
 7     return result //call函数有返回值
 8 }
 9 
10 function foo(){
11     console.log("foo调用了",this)
12 }
13 function sum(num1,num2){
14     return num1+num2
15 }
16 foo.myCall()
17 let re=sum.myCall({},20,30)
18 console.log(re)

apply

 1 Function.prototype.myApply=function(thisArg,argArray){
 2     let fn=this
 3     thisArg=(thisArg!==null&&thisArg!==undefined)?Object(thisArg):window
 4     thisArg.fn=fn
 5     let result
 6     //一:
 7     // if(!argArray){//没传值时
 8     //     result=thisArg.fn()
 9     // }else{//传值时
10     //     result=thisArg.fn(...argArray)
11     // }
12     //二:
13     // argArray=argArray?argArray:[]
14     //三:
15     argArray=argArray||[]
16     result=thisArg.fn(...argArray)
17     delete thisArg.fn
18     return result
19 }
20 
21 function foo(){
22     console.log("foo调用了",this)
23 }
24 function sum(num1,num2){
25     return num1+num2
26 }
27 foo.myApply({})
28 let re=sum.myApply({},[20,30])
29 console.log(re)

bind

 1 Function.prototype.myBind=function(thisArg,...argArray){
 2     //1.获取到真实需要调用的函数
 3     let fn=this
 4     //2.绑定this
 5     thisArg=(thisArg!==null&&thisArg!==undefined)?Object(thisArg):window
 6     function proxyFn(...arg){
 7         //3.将函数放到thisArg中进行调用
 8         thisArg.fn=fn
 9         //特殊:对两个传入的参数进行合并
10         let finalArg=[...argArray,...arg]
11         let result=thisArg.fn(...finalArg)
12         delete thisArg.fn
13         //4.返回结果
14         return result
15     }
16     return proxyFn
17 }
18 
19 function foo(){
20     console.log("foo执行了",this)
21 }
22 function sum(num1,num2, num3){
23     console.log(num1,num2,num3)
24     return num1+num2+num3
25 }
26 
27 let fn=foo.myBind({})
28 fn()
29 
30 let su=sum.myBind({},2,3)
31 let result=su(5)
32 console.log(result)

 

标签:function,let,bind,argArray,call,result,thisArg,apply,fn
From: https://www.cnblogs.com/KooTeam/p/17659376.html

相关文章

  • [远程Call]32位远程多参数带返回调用
    [远程Call]32位远程多参数带返回调用引子在Windows上可以使用CreateRemoteThread实现远程Call,但是有不带返回值且只能传递一个参数的限制。解决思路将多个参数利用VirtualAllocEx和WriteProcessMemory写入目标程序,再通过此方法注入一段shellcode,通过shellcode完成多参数的......
  • Spring源码搭建导依赖时报错:Failed to apply plugin 'kotlin'.
    原因是kotlin插件的版本与gradle中指定的版本不一致,我的是1.8.0,spring5.3.x版本gradle配置文件指定的kotlin版本是1.5.32,修改成1.8.0......
  • 并发问题和实现Callable接口
    并发1.初识并发问题//多个线程同时操作一个对象//买火车票//发现问题:多个线程操作同一个资源的情况下,线程不安全publicclassTestThread4implementsRunnable{  //票数  privateintticketNums=10;​  publicvoidrun(){    while(true){  ......
  • org.apache.jasper.servlet.TldScanner$TldScannerCallback.scan(Lorg/apache/tomcat/
    原因<dependency><groupId>org.apache.hive</groupId><artifactId>hive-jdbc</artifactId><version>3.1.3</version></dependency>解决<dependency>......
  • Pybind11:使用C++编写Python模块
    放假摆了一周了。看论文实在不是什么有意思的活。这两天研究了一下Pybind11的用法。使用C/C++和Python混合编程的想法很早就有了,在大一的一次比赛时曾经实践过(虽然不是我写的),当时获得了比较显著的性能提升。但是当时用的是Swig,据队友说Swig对于NumPy的支持极为阴间,当时调试花了好......
  • Binder原理
    从进程角度看IPC机制每个Android的进程,只能运行在自己进程所拥有的的虚拟地址空间。对应一个4GB大小的虚拟地址空间,其中3GB是用户空间,1GB是内核空间,内核空间的大小是可以通过参数配置调整的。对于用户空间,不同进程之间彼此是不能共享的,而内核空间却是可以共享的。Client进程向Serve......
  • 解决getOutputStream() has already been called for this response
    getOutputStream()hasalreadybeencalledforthisresponse异常出现的原因和解决方法:jsp中出现此错误一般都是在jsp中使用了输出流(如输出图片验证码,文件下载等),没有妥善处理好的原因。具体的原因:jsp编译成servlet之后在函数_jspService(HttpServletRequestrequest,HttpServlet......
  • MIT6.s081/6.828 lectrue5/6:System call entry/exit 以及 Lab4 心得
    这篇博客主要复习lecture05:GDBcallingconentions和lecture06:Systemcallentry/exit的内容,外加Lab4:traps的心得前置知识这里的前置知识是指lecture05:GDBcallingconentions的内容,是由TA来上的,是作为lecture06的前置知识,主要讲解了以下三点内容:指令集架构的概念......
  • Callback Function Essence
    IncludeExampleInput:Iama.routeexecutefinish.Iamb.routeexecutefinish.WhatisCallbackCallbackfunctiondefine:Ifafunctionisthreatedasafunctionparameter,thenthefunctionnamedaCallbackfunction.Callbackfunctionisaverycom......
  • 大厂 Framework 面试必备 Handler&Binder&AMS 面试题
    前言大家都知道现在AndroidFramework成为头部公司必不缺少的技术栈之一,尤其是熟悉Franmework源码的Android开发者,在面试中往往会占到很大的优势那我今天就带来AndroidFramework比较高刷的面试题分享,总共包含以下四大类:系统启动流程面试题解析Binder面试题解析Handler面......