当前位置: 首页 > news >正文

call apply bind特性及手动实现

call

// 原生的call
var foo = { value: 1 };function bar(...args) {console.log("this", this.value, args);
}bar.call(foo)// call 改变了bar的this指向
// bar函数执行了
// 等价于
// var foo = {
//   name: "tengzhu",
//   sex: "man",
//   bar() {
//     console.log("this", this);
//   },
// };
  • 手动实现call
var foo = { value: 1 };function bar(...args) {console.log("this", this.value, args);
}// 1. 将函数设置为对象的属性
// 2. 执行该函数
// 3. 删除该函数
// 4. 接受多个参数
Function.prototype.myCall = function (context, ...args) {// this参数为null 的时候 ,指向window// context === foo// args 为 bar的入参const ctx = context || window;const symbol = Symbol();ctx[symbol] = this;// const args = [...arguments].slice(1);const result = ctx[symbol](...args);delete ctx[symbol];return result;
};bar.myCall(foo, 1, 2, 3);
  • 手动实现apply
var foo = { value: 1 };function bar(...args) {console.log("this", this.value, args);
}// 1. 将函数设置为对象的属性
// 2. 执行该函数
// 3. 删除该函数
// 4. 接受两个入参,第一个为要指向的对象,数组
Function.prototype.myApply = function (context, args) {// this参数为null 的时候 ,指向window// context === foo// args 为 bar的入参const ctx = context || window;const symbol = Symbol();ctx[symbol] = this;// const args = [...arguments].slice(1);const result = ctx[symbol](...args);delete ctx[symbol];return result;
};bar.myApply(foo, [1, 2, 3]);
  • 手动实现bind

基础版

// 1. 返回该函数
// 2. 接收多个入参,并且主动调用时的入参也生效
Function.prototype.myBind = function (context, ...args) {const _self = this; // barreturn function (...newArgs) {return _self.myApply(context, [...args, ...newArgs]);};
};const bindFoo = bar.myBind(foo, 1, 2, 3);
bindFoo(4, 5, 6);

通过new bindFoo()使用

const foo = { value: 1 };
function bar(name, age) {this.habbit = "basketball";console.log("this", name, age);
}
bar.prototype.friend = "xiaoming";// 手写bind
// 1. 一个绑定函数也能使用new操作符创建对象:这种行为就像把原函数当成构造器。提供的 this 值被忽略,同时调用时的参数被提供给模拟函数
// * 当 bind 返回的函数作为构造函数的时候,bind 时指定的 this 值会失效,但传入的参数依然生效
Function.prototype.myBind = function (context, ...args) {// 判断是否是undefined 和 nullif (typeof context === "undefined" || context === null) {context = window;}const _self = this; // barconst middleFn = function () {}; // 中转空函数const returnFn = function (...newArgs) {// 当作为构造函数时,this 指向实例,此时结果为 true,将绑定函数的 this 指向该实例,可以让实例获得来自绑定函数的值// 当作为普通函数时,this 指向 window,此时结果为 false,将绑定函数的 this 指向 contextconst ctx = this instanceof returnFn ? this : context; // 判断this是否失效return _self.myApply(ctx, [...args, ...newArgs]);};// 修改返回函数的 prototype 为绑定函数的 prototype,实例就可以继承绑定函数的原型中的值// 方法一:// 原型链共享 影响父层原型(即修改returnFn.prototype上的属性会影响父级(bar)的原型):bar.prototype.friend 会变成xiaohong// returnFn.prototype = _self.prototype; // => returnFn.prototype = _self.prototype// returnFn.prototype.friend = "xiaohong";// 方法二:// 通过一个空函数中转,此时修改returnFn.prototype上的属性时,只是修改了returnFn本身,不会干扰父级_self.prototypemiddleFn.prototype = _self.prototype;returnFn.prototype = new middleFn(); // => returnFn.prototype.__proto__ = _self.prototypereturn returnFn;
};// const test = bar.myBind(foo, "xiaoshuai", 19);
// test(4, 5, 7);
const bindFoo = bar.myBind(foo, "xiaoshuai", 19);
const test = new bindFoo(9);console.log("test", test, test.friend, bar.prototype.friend);
http://www.lryc.cn/news/428488.html

相关文章:

  • pygame开发课程系列(5): 游戏逻辑
  • 嵌入式系统实时任务调度算法优化与实现
  • Java:枚举转换
  • Vue、react父子组件生命周期
  • HTML 基础要素解析
  • 开源的向量数据库Milvus
  • 设计模式-工厂方法
  • Flask SQLALchemy 的使用
  • Metasploit漏洞利用系列(一):MSF完美升级及目录结构深度解读
  • C/C++|经典代码题(动态资源的双重释放与「按值传递、按引用传递、智能指针的使用」)
  • 西北乱跑娃 -- linux使用笔记
  • Kubectl基础命令使用
  • 推荐编译器插件:Fitten Code 更快更好的AI助手
  • ArcGIS Pro基础:状态栏显示栏的比例尺设置和经纬度位置
  • 微前端架构入门
  • [LitCTF 2023]导弹迷踪
  • win10安装wsl2(ubuntu20.04)并安装 TensorRT-8.6.1.6、cuda_11.6、cudnn
  • 信息搜集--敏感文件Banner
  • Qt 学习第六天:页面布局
  • 利用队列收集单双击和长按按键
  • AI工作流:低代码时代的革新者,重塑手机问答类应用生态
  • 配置MySQL主从,配置MySQL主主 +keeplive高可用
  • 第5节:Elasticsearch核心概念
  • 存储实验:华为异构存储在线接管与在线数据迁移(Smart Virtualization Smart Migration 特性)
  • 职业院校云计算实训室建设方案全景剖析
  • VS Code安装与vue项目新建
  • 如何在Java中将数据库查询结果转换为枚举类型
  • 秋招突击——8、20——知识补充——Java容器
  • IOS 06 OC调用Swift第三方框架
  • SAP和致远OA系统集成案例