itsjoekent-bind
Version:
Bind functions to a scope
57 lines (49 loc) • 1.3 kB
JavaScript
;
/**
* Bind the given list of function names on the Class
* to the class.
*
* @param {Object} context
* @param {Array<String>} functions
*/
function functions(context, functions) {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = functions[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var fn = _step.value;
if (!context[fn]) continue;
context[fn] = context[fn].bind(context);
context[fn].isBinded = true;
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
/**
* Bind every function on the given class to the class.
*
* @param {Object} context
* @param {Class} clss
*/
function all(context, clss) {
var allFunctions = Object.getOwnPropertyNames(clss.prototype).filter(function (fn) {
return fn !== 'constructor';
});
functions(context, allFunctions);
}
module.exports = {
functions: functions, all: all
};