toolkit/components/url-classifier/content/moz/lang.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:42dbc4ef06e8
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5
6 /**
7 * lang.js - Some missing JavaScript language features
8 */
9
10 /**
11 * Partially applies a function to a particular "this object" and zero or
12 * more arguments. The result is a new function with some arguments of the first
13 * function pre-filled and the value of |this| "pre-specified".
14 *
15 * Remaining arguments specified at call-time are appended to the pre-
16 * specified ones.
17 *
18 * Usage:
19 * var barMethBound = BindToObject(myFunction, myObj, "arg1", "arg2");
20 * barMethBound("arg3", "arg4");
21 *
22 * @param fn {string} Reference to the function to be bound
23 *
24 * @param self {object} Specifies the object which |this| should point to
25 * when the function is run. If the value is null or undefined, it will default
26 * to the global object.
27 *
28 * @returns {function} A partially-applied form of the speficied function.
29 */
30 function BindToObject(fn, self, opt_args) {
31 var boundargs = fn.boundArgs_ || [];
32 boundargs = boundargs.concat(Array.slice(arguments, 2, arguments.length));
33
34 if (fn.boundSelf_)
35 self = fn.boundSelf_;
36 if (fn.boundFn_)
37 fn = fn.boundFn_;
38
39 var newfn = function() {
40 // Combine the static args and the new args into one big array
41 var args = boundargs.concat(Array.slice(arguments));
42 return fn.apply(self, args);
43 }
44
45 newfn.boundArgs_ = boundargs;
46 newfn.boundSelf_ = self;
47 newfn.boundFn_ = fn;
48
49 return newfn;
50 }
51
52 /**
53 * Inherit the prototype methods from one constructor into another.
54 *
55 * Usage:
56 *
57 * function ParentClass(a, b) { }
58 * ParentClass.prototype.foo = function(a) { }
59 *
60 * function ChildClass(a, b, c) {
61 * ParentClass.call(this, a, b);
62 * }
63 *
64 * ChildClass.inherits(ParentClass);
65 *
66 * var child = new ChildClass("a", "b", "see");
67 * child.foo(); // works
68 *
69 * In addition, a superclass' implementation of a method can be invoked
70 * as follows:
71 *
72 * ChildClass.prototype.foo = function(a) {
73 * ChildClass.superClass_.foo.call(this, a);
74 * // other code
75 * };
76 */
77 Function.prototype.inherits = function(parentCtor) {
78 var tempCtor = function(){};
79 tempCtor.prototype = parentCtor.prototype;
80 this.superClass_ = parentCtor.prototype;
81 this.prototype = new tempCtor();
82 }

mercurial