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

手机模板网站模板下载网站怎么做网站导航栏

手机模板网站模板下载网站,怎么做网站导航栏,企业解决方案图片,建设网站需要的技术在 JavaScript 中,bind、call 和 apply 方法都可以用来改变函数的 this 指向。下面我们将分别实现这些方法的简单版本。 1. 实现 bind bind 方法创建一个新的函数,在调用时设置 this 值,并返回这个新的函数。 Function.prototype.myBind …

在 JavaScript 中,bindcallapply 方法都可以用来改变函数的 this 指向。下面我们将分别实现这些方法的简单版本。

1. 实现 bind

bind 方法创建一个新的函数,在调用时设置 this 值,并返回这个新的函数。

Function.prototype.myBind = function (context) {if (typeof this !== 'function') {throw new TypeError('Not a function');}const fn = this;const args = Array.prototype.slice.call(arguments, 1);return function bound() {const boundArgs = Array.prototype.slice.call(arguments);return fn.apply(context, args.concat(boundArgs));};
};

2. 实现 call

call 方法立即调用函数,并设置 this 值,同时传递参数列表。

Function.prototype.myCall = function (context) {if (typeof this !== 'function') {throw new TypeError('Not a function');}context = context || window;const args = Array.prototype.slice.call(arguments, 1);context.fn = this;const result = context.fn(...args);delete context.fn;return result;
};

3. 实现 apply

apply 方法与 call 类似,但传递的参数是一个数组。

Function.prototype.myApply = function (context, args) {if (typeof this !== 'function') {throw new TypeError('Not a function');}context = context || window;args = args || [];context.fn = this;const result = context.fn(...args);delete context.fn;return result;
};

示例代码

下面是一个完整的示例,展示了如何使用这些自定义的方法:

<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>自定义 bind, call, apply</title>
</head>
<body><script type="text/javascript">Function.prototype.myBind = function (context) {if (typeof this !== 'function') {throw new TypeError('Not a function');}const fn = this;const args = Array.prototype.slice.call(arguments, 1);return function bound() {const boundArgs = Array.prototype.slice.call(arguments);return fn.apply(context, args.concat(boundArgs));};};Function.prototype.myCall = function (context) {if (typeof this !== 'function') {throw new TypeError('Not a function');}context = context || window;const args = Array.prototype.slice.call(arguments, 1);context.fn = this;const result = context.fn(...args);delete context.fn;return result;};Function.prototype.myApply = function (context, args) {if (typeof this !== 'function') {throw new TypeError('Not a function');}context = context || window;args = args || [];context.fn = this;const result = context.fn(...args);delete context.fn;return result;};// 测试对象const obj = {name: '牛客网'};// 测试函数function greet(message) {console.log(`${message}, ${this.name}`);}// 使用 myBindconst greetBound = greet.myBind(obj, '欢迎来到');greetBound(); // 输出: 欢迎来到, 牛客网// 使用 myCallgreet.myCall(obj, '欢迎来到'); // 输出: 欢迎来到, 牛客网// 使用 myApplygreet.myApply(obj, ['欢迎来到']); // 输出: 欢迎来到, 牛客网</script>
</body>
</html>

详细步骤

  1. 实现 myBind

    • 检查调用者是否为函数。
    • 获取上下文 context 和传入的参数 args
    • 返回一个新的函数 bound,在调用时使用 apply 方法设置 this 值并传递参数。
  2. 实现 myCall

    • 检查调用者是否为函数。
    • 获取上下文 context 和传入的参数 args
    • 将函数赋值给 context 的一个临时属性 fn,调用该属性并传递参数,然后删除该属性。
  3. 实现 myApply

    • 检查调用者是否为函数。
    • 获取上下文 context 和传入的参数数组 args
    • 将函数赋值给 context 的一个临时属性 fn,调用该属性并传递参数,然后删除该属性。

测试

  1. 使用 myBind

    • 创建一个绑定了 obj 上下文的新函数 greetBound,并调用它。
  2. 使用 myCall

    • 直接调用 greet 函数,并设置 objthis 值。
  3. 使用 myApply

    • 直接调用 greet 函数,并设置 objthis 值,参数以数组形式传递。
http://www.yayakq.cn/news/890439/

相关文章:

  • 宝安网站设计服务自己能开发app软件吗
  • 微信手机网站开发南昌网站app开发
  • 做网站的费用是多少钱wordpress 站点
  • 高要住房和城乡建设局网站wordpress评论字段
  • 网站seo 优化网站建设与维护报告总结
  • 做关于星空的网站快站建站
  • 网站开发常用中间件企业咨询公司管理
  • 云南建设注册考试中心网站app网页版入口
  • 设计最好的网站汽车最专业的网站建设
  • 网站ip和pv陕西 网站备案
  • 搭建网站平台有前途吗宿迁西楚房产网
  • 网站建设动态部分实训报告网页游戏中心大全
  • 北京网站建设降龙网络网站图片一般像素
  • 建设网站常见问题经营网站备案
  • 建立公司网站的目的php中switch做网站
  • 做网站青岛旅游网页模板图片
  • 备用网站域名重庆的网站建设公司
  • 外贸建站什么样的水平可以做网站
  • 做房地产策划需要关注的网站怎么自己做网站表白
  • 旅游网站开发背景论文seo快排优化
  • 网站毕设wordpress 发布html
  • 基础网站怎么做苏州怎么做网站排名优化
  • 网站缩略图存哪里好制作网页框架
  • 绵阳市城市建设档案馆网站网站科技动效
  • cad图纸免费下载网站wordpress+仿站步骤
  • seo单页面wordpress中山seo建站
  • 做外包任务网站官方网站welcome
  • 电商网站建设实训步骤搜狗推广效果好吗
  • 网站还需要备案么网站设计制作的服务机构
  • 做进口货的电商网站金泉网做网站找谁