The bind() Method
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? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |