加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

The bind() Method

发布时间:2020-12-14 04:31:08 所属栏目:大数据 来源:网络整理
导读:The bind() method was added in ESMAScript 5,but it is easy to simulate in ESMAScrpt 3. As its name implies,the primary purpose of bind() is to bind a function to an object. When you invoke the bind() method on a function f and pass an obje

The bind() method was added in ESMAScript 5,but it is easy to simulate in ESMAScrpt 3. As its name implies,the primary purpose of bind() is to bind a function to an object. When you invoke the bind() method on a function f and pass an object o,the method returns a new function. Invoking the new function (as a function) invokes the original function f as a method of o. Any arguments you pass to the new function are passed to the original function. For example:? ? ? ? ? ? ? ? ? ?

? ? ? ? ? function f(y) { return this.x + y; }? // This function need to be bound? ? ? ? ? var o = { x:1 };? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//? An object we‘ll bind to? ? ? ? ? var g = f.bind(o);? ? ? ? ? ? ? ? ? ? ? ? ?//? Calling g(x) invokes o.f(x)? ? ? ? ? g(2);? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //? => 3? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读