当前位置: 首页 > 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.yidumall.com/news/50672.html

相关文章:

  • 怎样推广自己做的网站台州seo排名公司
  • 佛山专业网站建设网站排名查询平台
  • 场外期权网站开发东莞做网站最好的是哪家
  • 能想到的域名都被注册了河北seo公司
  • wordpress发布十万篇文章seo流量是什么意思
  • 哪里做网站的seo搜索规则
  • 自己做网站咋做奇葩网站100个
  • maigoo网编筛选了部分相对主流的平面及室内设计网站深圳企业网站制作公司
  • 天远软件网站建设百度收录教程
  • 做网站需要美工吗seo优化网站百度技术
  • 青岛网站优化广州搜索排名优化
  • 个人做网站如何推广网络营销公司名字大全
  • 平台营销型网站建设怎么做优化关键词
  • 阜宁做网站找哪家好网站建设流程是什么
  • 坪山网站建设信息广州网站优化排名
  • 做导购网站赚钱吗数字营销是干啥的
  • 做网站付费流程精准营销平台
  • 个人空间网站免费培训机构专业
  • 精品应用下载安装seo运营招聘
  • 做垃圾词影响网站排名吗搜索引擎营销的主要方式有
  • 东莞做网站公司广告推广投放平台
  • 免费咨询离婚律师在线福州seo快速排名软件
  • 网站怎样做自适应分辨率大小成功营销案例100例
  • 泉州市住房和城乡建设局网站网页搜索关键字
  • 湖北seo优化的含义
  • 网页设计与网站架设海口网站排名提升
  • 免费凡科建站官网河南网站网络营销推广
  • 光遇网页制作素材杭州seo网站优化公司
  • 品牌网站开发广州seo推广公司
  • 做图像网站2020年度关键词有哪些