| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // The following code is taken from: | |
| 6 // http://code.google.com/p/browsersync/source/browse/trunk/client/lib/base/lang
.js | |
| 7 | |
| 8 /** | |
| 9 * Partially applies this function to a particular "this object" and zero or | |
| 10 * more arguments. The result is a new function with some arguments of the first | |
| 11 * function pre-filled and the value of |this| "pre-specified". | |
| 12 * | |
| 13 * Remaining arguments specified at call-time are appended to the pre- | |
| 14 * specified ones. | |
| 15 * | |
| 16 * Also see: partial(). | |
| 17 * | |
| 18 * Note that bind and partial are optimized such that repeated calls to it do | |
| 19 * not create more than one function object, so there is no additional cost for | |
| 20 * something like: | |
| 21 * | |
| 22 * var g = bind(f, obj); | |
| 23 * var h = partial(g, 1, 2, 3); | |
| 24 * var k = partial(h, a, b, c); | |
| 25 * | |
| 26 * Usage: | |
| 27 * var barMethBound = bind(myFunction, myObj, "arg1", "arg2"); | |
| 28 * barMethBound("arg3", "arg4"); | |
| 29 * | |
| 30 * @param self {Object} Specifies the object which |this| should point to | |
| 31 * when the function is run. If the value is null or undefined, it will default | |
| 32 * to the global object. | |
| 33 * | |
| 34 * @returns {Function} A partially-applied form of the function bind() was | |
| 35 * invoked as a method of. | |
| 36 */ | |
| 37 function bind(fn, self, var_args) { | |
| 38 var boundargs = fn.boundArgs_ || []; | |
| 39 boundargs = boundargs.concat(Array.prototype.slice.call(arguments, 2)); | |
| 40 | |
| 41 if (typeof fn.boundSelf_ != "undefined") { | |
| 42 self = fn.boundSelf_; | |
| 43 } | |
| 44 | |
| 45 if (typeof fn.boundFn_ != "undefined") { | |
| 46 fn = fn.boundFn_; | |
| 47 } | |
| 48 | |
| 49 var newfn = function() { | |
| 50 // Combine the static args and the new args into one big array | |
| 51 var args = boundargs.concat(Array.prototype.slice.call(arguments)); | |
| 52 return fn.apply(self, args); | |
| 53 } | |
| 54 | |
| 55 newfn.boundArgs_ = boundargs; | |
| 56 newfn.boundSelf_ = self; | |
| 57 newfn.boundFn_ = fn; | |
| 58 | |
| 59 return newfn; | |
| 60 } | |
| 61 | |
| 62 /** | |
| 63 * An alias to the bind() global function. | |
| 64 * | |
| 65 * Usage: | |
| 66 * var g = f.bind(obj, arg1, arg2); | |
| 67 * g(arg3, arg4); | |
| 68 */ | |
| 69 Function.prototype.bind = function(self, var_args) { | |
| 70 return bind.apply( | |
| 71 null, [this, self].concat(Array.prototype.slice.call(arguments, 1))); | |
| 72 } | |
| OLD | NEW |