UNPKG

proteus

Version:

A declarative way of creating objects, properties, and classes in ES5 JavaScript

48 lines (44 loc) 1.54 kB
var putil = require("proteus/util"), slice = putil.slice ; module.exports = { /** * Return a function that is bound to call another function on the current * object, or the supplied one. * * @method aliasMethod * @param {String} name Name of the method to alias * @param {Object} [scope] Object in which the function resides, * and the scope to call the method in. * @return {Function} */ aliasMethod: function (name, scope) { var args = putil.slice(arguments, 2); // NOTE: We do not use 'bind' because the named method may not exist // at the time this is invoked. return function () { var self = scope || this, fn = typeof name === "function" ? name : self[name] ; if (!fn) { throw Error("Alias function '" + name + "' does not exist."); } return fn.apply(self, args.concat(putil.slice(arguments))); }; }, /** * Delegate a function call to another object. * * @method delegateMethod * @param {Object} obj The object to delegate to * @param {String} name Name of the function to delegate to on 'obj' * @param {Mixed} rest Additional arguments to prepend to the function call * @return {Function} */ delegateMethod: function (obj, name /*, rest */) { var args = slice(arguments, 2), fn = obj[name] ; return fn.bind.apply(fn, [obj].concat(args)); } };