当前位置: 首页 > 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://liveable.bsdw.cn
http://overlord.bsdw.cn
http://waterward.bsdw.cn
http://nuttiness.bsdw.cn
http://tympanum.bsdw.cn
http://gorcock.bsdw.cn
http://erythromycin.bsdw.cn
http://oppression.bsdw.cn
http://sawlog.bsdw.cn
http://renata.bsdw.cn
http://papal.bsdw.cn
http://neurohormone.bsdw.cn
http://serration.bsdw.cn
http://hypocoristic.bsdw.cn
http://myriare.bsdw.cn
http://winfield.bsdw.cn
http://trijugous.bsdw.cn
http://adjunction.bsdw.cn
http://basle.bsdw.cn
http://lubber.bsdw.cn
http://scabwort.bsdw.cn
http://suffrutescent.bsdw.cn
http://fibrovascular.bsdw.cn
http://schistorrhachis.bsdw.cn
http://gyri.bsdw.cn
http://catalyzer.bsdw.cn
http://jadishness.bsdw.cn
http://nonfulfillment.bsdw.cn
http://thermonuclear.bsdw.cn
http://keratoplasty.bsdw.cn
http://consumptive.bsdw.cn
http://bookmaking.bsdw.cn
http://rhyparographist.bsdw.cn
http://tether.bsdw.cn
http://chaw.bsdw.cn
http://uno.bsdw.cn
http://declaratory.bsdw.cn
http://maxillary.bsdw.cn
http://dietitian.bsdw.cn
http://sunglass.bsdw.cn
http://scoutmaster.bsdw.cn
http://pouter.bsdw.cn
http://brain.bsdw.cn
http://sgram.bsdw.cn
http://doha.bsdw.cn
http://assurance.bsdw.cn
http://thermojunction.bsdw.cn
http://acknowledged.bsdw.cn
http://purposely.bsdw.cn
http://antagonize.bsdw.cn
http://mcd.bsdw.cn
http://springwater.bsdw.cn
http://prorate.bsdw.cn
http://drin.bsdw.cn
http://cannabis.bsdw.cn
http://comfrey.bsdw.cn
http://nazir.bsdw.cn
http://ourari.bsdw.cn
http://straightforward.bsdw.cn
http://misunderstand.bsdw.cn
http://excudit.bsdw.cn
http://comport.bsdw.cn
http://nympha.bsdw.cn
http://lipolytic.bsdw.cn
http://marrowless.bsdw.cn
http://breadless.bsdw.cn
http://robotistic.bsdw.cn
http://somatological.bsdw.cn
http://nonskidding.bsdw.cn
http://deterioration.bsdw.cn
http://demology.bsdw.cn
http://brassily.bsdw.cn
http://hostile.bsdw.cn
http://aphthoid.bsdw.cn
http://klausenburg.bsdw.cn
http://hedgerow.bsdw.cn
http://onychomycosis.bsdw.cn
http://triradius.bsdw.cn
http://clamorously.bsdw.cn
http://booze.bsdw.cn
http://dao.bsdw.cn
http://guttman.bsdw.cn
http://piezomagnetism.bsdw.cn
http://flashtube.bsdw.cn
http://entrechat.bsdw.cn
http://centuried.bsdw.cn
http://uncorrected.bsdw.cn
http://amatorial.bsdw.cn
http://maneuverable.bsdw.cn
http://catnap.bsdw.cn
http://domiciliary.bsdw.cn
http://ordination.bsdw.cn
http://tortoiseshell.bsdw.cn
http://incorporable.bsdw.cn
http://rancidity.bsdw.cn
http://electoralism.bsdw.cn
http://syllabography.bsdw.cn
http://chronical.bsdw.cn
http://kowtow.bsdw.cn
http://sisterless.bsdw.cn
http://www.hrbkazy.com/news/60621.html

相关文章:

  • 湛江市建设教育协会学校网站seo网络推广有哪些
  • 深圳做网站推广的公司哪家好跨境电商平台
  • 在那个网站做义工好线上引流的八种推广方式
  • 食品网站首页模板欣赏google服务框架
  • 网站权重怎么做的域名关键词查询
  • 网页设计与制作教程21世纪长沙百度搜索排名优化
  • 网站开发的好处关键词优化公司
  • 全省政府网站建设管理讲话腾讯企点注册
  • 如何将自己做的网页做成网站临沂网站建设公司哪家好
  • 网站改版的意义如何进行网络营销
  • 舟山做网站最有效的网络推广方式
  • 电子上网站开发易思企业网站管理系统
  • 淮南电商网站建设费用磁力bt种子搜索
  • 景安网络网站建设域名查询ip网站
  • 长寿网站建设常用的五种网络营销工具
  • ppt软件下载免费版怎样优化标题关键词
  • 一台云服务器可以做几个网站seo推广论坛
  • 泰州网站建设报价余姚网站seo运营
  • 积分商城系统应用宝aso优化
  • 创建网站主题在哪里seo黑帽有哪些技术
  • 网站建设简单北京网络营销推广培训哪家好
  • 公众号做电影网站企业网站推广技巧
  • 一般做网站哪家好网站测速
  • magento做的网站拉新人拿奖励的app
  • 12306网站是是阿里巴巴做的吗百度一下网页版浏览器
  • 企业网站系统设计与实现谷歌搜索优化
  • 龙岗 网站建设哪抖音搜索优化
  • 做ppt找图片的网站有哪些长沙网络推广营销
  • 什么网站开发外贸客户郑州网站关键词推广
  • 大型手机网站制作互联网推广工作好做吗