File: hitch.js

package info (click to toggle)
greasemonkey 2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 1,820 kB
  • sloc: xml: 171; makefile: 10
file content (29 lines) | stat: -rw-r--r-- 899 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const EXPORTED_SYMBOLS = ['hitch'];

function hitch(obj, method) {
  if (obj && method && ('string' == typeof method)) {
    if (!obj[method]) {
      throw "method '" + method + "' does not exist on object '" + obj + "'";
    }
    method = obj[method];
  } else if ('function' == typeof method) {
    obj = obj || {};
  } else {
    throw "Invalid arguments to GM_util.hitch().";
  }

  var staticArgs = Array.prototype.splice.call(arguments, 2, arguments.length);

  return function() {
    // make a copy of staticArgs (don't modify it because it gets reused for
    // every invocation).
    var args = Array.prototype.slice.call(staticArgs);

    // add all the new arguments
    Array.prototype.push.apply(args, arguments);

    // invoke the original function with the correct this obj and the combined
    // list of static and dynamic arguments.
    return method.apply(obj, args);
  };
}