首页 > 其他分享 >手写 call、applay

手写 call、applay

时间:2023-03-30 16:22:08浏览次数:28  
标签:Function ... args applay call result context 手写 fn

 

call

Function.prototype.mycall = function(context, ...args) {
  if (this === Function.prototype) {
    return undefined;
  }
  context = context || window;
  const fn = Symbol();
  context[fn] = this; // this就是 调用的函数
  const result = context[fn](...args); // 
  delete context[fn];
  return result;
}

实现逻辑如下,把函数给这个对象,然后通过对象的方式调用,this 就指向这个 对象了

function test(...args) {
  console.log(this.name, ...args);
}
let obj = {
  name: 'lisi'
}
obj.fn = test;
obj.fn(1,2);
apply 只是参数处理不一样
Function.prototype.myapply = function(context, args) {
  if (this === Function.prototype) {
    return undefined;
  }
  context = context || window;
  const fn = Symbol();
  context[fn] = this; // this就是 调用的函数
  const result = context[fn](...args); // 
  delete context[fn];
  return result;
}

 

标签:Function,...,args,applay,call,result,context,手写,fn
From: https://www.cnblogs.com/bruce-gou/p/17273261.html

相关文章

  • 手写bind函数
     Function.prototype.myBind_3=function(){letoutContext=arguments[0]//取上下文letoutArgs=Array.from(arguments).slice(1)//取外部入参......
  • 如何在Go的函数中得到调用者函数名(caller)
    在go语言中,可以通过runtimepackage中Caller函数获取调用者信息funcCaller(skipint)(pcuintptr,filestring,lineint,okbool)skip表示查看第几层调用栈信息......
  • 基于CNN卷积神经网络的minst数据库手写字识别matlab仿真
    1.算法描述深度学习(DL,DeepLearning)是机器学习(ML,MachineLearning)领域中一个新的研究方向,它被引入机器学习使其更接近于最初的目标——人工智能(AI,ArtificialIn......
  • 随手写的一个 DataV代码 写到一半写不动了 弃坑!~
    !(function(v,g){g["DataV"]||(g["DataV"]=v());})(function(){constzoom=[0,20,40,60,80,99];//获取唯一序列码letxid_i......
  • wine 运行Call of Duty Modern Warfare 2以及starcraft2方法
    必需条件:wine升级到1.3以上Linux已经正确安装显卡驱动其他需要东西:DirectX以及VC运行库这些东西请自己准备吧,不需要我一个一个说了然后关键是用wineregedit导入下面注册......
  • Promise 基础知识及手写简易Promise
    promise引用类型Promise,翻译期约(承诺),是一种异步编程结局方案。当我们许下承诺,代表着未来的不确定性(pending),当我们实现时,承诺变为成功(fulfilled)。当我们未能实现时,承诺变......
  • QT | 手写代码实现HelloWorld
    QT|手写代码实现HelloWorld文章目录`QT`|手写代码实现`HelloWorld`1.新建工程1-1.main.cpp文件1-2.mainwindow.h和mainwindow.cpp文件1-3.编译、运行2.编码实现简易的......
  • cocos2d-x CCCallFuncN中node CCCallFuncND中data
    CCCallFuncN:1CCCallFuncN*instant=newCCCallFuncN;2instant->initWithTarget(this,callfuncN_selector(ActionCallFunc::callback2));3m_grossini......
  • JAVA异步编程之Callbacks与Futures模型
    JAVA异步编程之Callbacks与Futures模型一:Callbacks模型​该模型的异步方法,在异步任务完成之后调用,主线程没有异步线程的结果。经典模型如Swing’sEventListener......
  • KiFastCallEntry Hook
    KiFastCallEntry函数有什么用???Ring0层Zw系列函数(如ZwSetEvent)在设置完函数服务号之后会调用KiSystemService函数,在KiSystemService函数中又会跳转到KiFastCallEntry函数......