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

崇明手机网站建设公司网页制作流程

崇明手机网站建设,公司网页制作流程,搜索引擎营销案例,html5做网站的代码在面试中,手写call,apply,bind是常见的题目,主要考察对JavaScript中函数上下文(this)和参数传递的理解。对于我这个小菜鸟来说,这无疑也是一道难题呢,嘤嘤嘤 1. 手写 call call 方法…

在面试中,手写call,apply,bind是常见的题目,主要考察对JavaScript中函数上下文(this)和参数传递的理解。对于我这个小菜鸟来说,这无疑也是一道难题呢,嘤嘤嘤


1. 手写 call

call 方法允许你调用一个函数,并显式地指定函数内部的 this 值,同时可以逐个传递参数。

实现思路:

  1. 将目标函数绑定到指定的上下文对象(thisArg)。

  2. 调用目标函数。

  3. 返回目标函数的执行结果。

代码实现:

Function.prototype.myCall=function(thisArg,...args){if(typeof this!=='function'){//如果调用者不是一个函数,则抛出错误throw new TypeError("myCall must be called on a function");}thisArg = thisArg||(thisArg===null?globalThis:thisArg)thisArg['fn']=this    thisArg['fn'](...args)// 这里相当于立即执行函数delete thisArg['fn']}

测试:


function greet(...message){console.log(`${message},my name is ${this.name}`)
}const person = {name:'alita'}
greet.myCall(person,'hello','hi','嘻嘻')//hello,hi,嘻嘻,my name is alita

2. 手写 apply

apply 方法与 call 类似,但参数是以数组的形式传递的。

实现思路:

  1. 将目标函数绑定到指定的上下文对象(thisArg)。

  2. 调用目标函数。

  3. 返回目标函数的执行结果。

代码实现:

Function.prototype.myApply = function (thisArg, argsArray) {// 检查是否为函数if (typeof this !== "function") {throw new TypeError("myApply must be called on a function");}// 如果 thisArg 是 null 或 undefined,按照规范,this 应该指向全局对象thisArg = thisArg || (thisArg === null ? globalThis : Object(thisArg));// 将函数绑定到 thisArg 上const fn = Symbol("fn");thisArg[fn] = this;// 调用函数并获取结果const result = thisArg[fn](...argsArray);// 删除临时属性delete thisArg[fn];return result;
};

测试:

function greet(message) {console.log(`${message}, my name is ${this.name}`);
}const person = { name: "Alice" };
greet.myApply(person, ["Hello"]); // 输出: Hello, my name is Alice

3. 手写 bind

bind 方法用于创建一个新的函数,并将该函数的 this 值永久绑定到指定的对象上。与 callapply 不同,bind 不会立即调用函数,而是返回一个新的函数。

实现思路:

  1. 创建一个新的函数。

  2. 将目标函数的 this 值绑定到指定对象。

  3. 支持预绑定参数。

  4. 支持作为构造函数使用(可选)。

代码实现:

Function.prototype.myBind = function (thisArg, ...bindArgs) {// 检查是否为函数if (typeof this !== "function") {throw new TypeError("myBind must be called on a function");}const fn = this;// 返回一个新的函数const boundFunction = function (...callArgs) {// 合并预绑定参数和调用时的参数const finalArgs = bindArgs.concat(callArgs);// 如果作为构造函数调用,this 指向新创建的对象if (new.target) {return new fn(...finalArgs);} else {// 否则,this 指向绑定的对象return fn.apply(thisArg, finalArgs);}};// 修复绑定函数的原型链boundFunction.prototype = Object.create(fn.prototype);return boundFunction;
};

测试:

function greet(message) {console.log(`${message}, my name is ${this.name}`);
}const person = { name: "Alice" };
const greetAlice = greet.myBind(person, "Hello");
greetAlice(); // 输出: Hello, my name is Alice// 测试作为构造函数
function Person(name, age) {this.name = name;this.age = age;
}const createPerson = Person.myBind(null, "Alice");
const alice = new createPerson(25);
console.log(alice); // { name: "Alice", age: 25 }

总结

  1. callapply

    • 都用于立即调用函数并改变上下文。

    • 区别在于参数传递方式:call 是逐个传递,apply 是数组传递。

    • 实现时,主要通过临时属性绑定和调用目标函数。

  2. bind

    • 返回一个新的函数,this 值和参数被预绑定。

    • 支持延迟调用和作为构造函数使用。

    • 实现时需要处理 new 调用的情况,并修复原型链。

这些实现可以帮助你更好地理解 JavaScript 中的函数上下文和参数传递机制。

http://www.dtcms.com/wzjs/377925.html

相关文章:

  • 做金融资讯网站需要哪些牌照自己建立网站步骤
  • 政府网站建设的功能市场调研流程
  • 什么网站可以做期货海外市场推广方案
  • 销售管理软件排行上海企业优化
  • 落伍者论坛 做网站分析网站推广和优化的原因
  • 怎么在ppt上做网站视频号广告推广
  • 怎么用ps做网站ui集客营销软件官方网站
  • 免费ppt模板下载 知乎seo标题优化是什么意思
  • 溧阳有做网站的吗每日一则新闻摘抄
  • 在线办理营业执照优化师是做什么的
  • 上海想找人设计网站网络促销方案
  • 高端企业网站建设网店运营是做什么的
  • 网站 工商备案合肥网络优化推广公司
  • 网站怎样做外链网络营销案例具体分析
  • 重庆最便宜的网站建设吸引人的微信软文范例
  • 商务网站页面网店运营推广平台
  • 最贵网站建设阿里云万网域名购买
  • 昆明网站制作seo综合优化公司
  • 太平洋保险网站做的这么烂管理培训机构
  • 东安网站建设长春做网站推荐选吉网传媒好
  • 电子商务网站建设与管理基础seo需要什么技术
  • 学建站论坛sem和seo的区别
  • 网站建设翻译英文是什么2021年搜索引擎排名
  • 徐州做网站病毒式营销的案例
  • 网站建设与功能模块千锋教育靠谱吗
  • 做设计需要知道的几个网站吗百度快照投诉
  • 提升学历的正规机构有哪些抖音seo关键词优化排名
  • wordpress建设企业网站鹤岗网站seo
  • 可以上传高清图片的网站并做外链google收录查询
  • 网站的发布与推广方式知乎推广优化