-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
45 lines (42 loc) · 1.38 KB
/
index.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["module", "exports"], factory);
} else if (typeof exports !== "undefined") {
factory(module, exports);
} else {
var mod = {
exports: {}
};
factory(mod, mod.exports);
global.es6bindall = mod.exports;
}
})(this, function (module, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
/**
* es6BindAll Binds methods to their parent contexts, e.g. and ES6 class
* @param {class} context The context to which the methods will be bound. Normally an ES6 class.
* @param {array} methods An Array of methods to bind to the context.
* @return {null} Function returns nothing.
*/
function es6BindAll(context, methodNames) {
if (!Array.isArray(methodNames)) {
methodNames = [methodNames];
}
methodNames.map(function (method) {
try {
context[method] = context[method].bind(context);
} catch (e) {
var cName = context.name ? ", " + context.name : "";
var mName = typeof method === "function" ? method.name : method;
console.log("Error: " + e);
throw new Error("Cannot bind method " + mName + " to the supplied context" + cName);
}
return null;
});
}
exports.default = es6BindAll;
module.exports = exports["default"];
});