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
                                                           

猜你喜欢

转载自www.cnblogs.com/tsai-87/p/10859274.html