michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: michael@0: /** michael@0: * lang.js - Some missing JavaScript language features michael@0: */ michael@0: michael@0: /** michael@0: * Partially applies a function to a particular "this object" and zero or michael@0: * more arguments. The result is a new function with some arguments of the first michael@0: * function pre-filled and the value of |this| "pre-specified". michael@0: * michael@0: * Remaining arguments specified at call-time are appended to the pre- michael@0: * specified ones. michael@0: * michael@0: * Usage: michael@0: * var barMethBound = BindToObject(myFunction, myObj, "arg1", "arg2"); michael@0: * barMethBound("arg3", "arg4"); michael@0: * michael@0: * @param fn {string} Reference to the function to be bound michael@0: * michael@0: * @param self {object} Specifies the object which |this| should point to michael@0: * when the function is run. If the value is null or undefined, it will default michael@0: * to the global object. michael@0: * michael@0: * @returns {function} A partially-applied form of the speficied function. michael@0: */ michael@0: function BindToObject(fn, self, opt_args) { michael@0: var boundargs = fn.boundArgs_ || []; michael@0: boundargs = boundargs.concat(Array.slice(arguments, 2, arguments.length)); michael@0: michael@0: if (fn.boundSelf_) michael@0: self = fn.boundSelf_; michael@0: if (fn.boundFn_) michael@0: fn = fn.boundFn_; michael@0: michael@0: var newfn = function() { michael@0: // Combine the static args and the new args into one big array michael@0: var args = boundargs.concat(Array.slice(arguments)); michael@0: return fn.apply(self, args); michael@0: } michael@0: michael@0: newfn.boundArgs_ = boundargs; michael@0: newfn.boundSelf_ = self; michael@0: newfn.boundFn_ = fn; michael@0: michael@0: return newfn; michael@0: } michael@0: michael@0: /** michael@0: * Inherit the prototype methods from one constructor into another. michael@0: * michael@0: * Usage: michael@0: * michael@0: * function ParentClass(a, b) { } michael@0: * ParentClass.prototype.foo = function(a) { } michael@0: * michael@0: * function ChildClass(a, b, c) { michael@0: * ParentClass.call(this, a, b); michael@0: * } michael@0: * michael@0: * ChildClass.inherits(ParentClass); michael@0: * michael@0: * var child = new ChildClass("a", "b", "see"); michael@0: * child.foo(); // works michael@0: * michael@0: * In addition, a superclass' implementation of a method can be invoked michael@0: * as follows: michael@0: * michael@0: * ChildClass.prototype.foo = function(a) { michael@0: * ChildClass.superClass_.foo.call(this, a); michael@0: * // other code michael@0: * }; michael@0: */ michael@0: Function.prototype.inherits = function(parentCtor) { michael@0: var tempCtor = function(){}; michael@0: tempCtor.prototype = parentCtor.prototype; michael@0: this.superClass_ = parentCtor.prototype; michael@0: this.prototype = new tempCtor(); michael@0: }