Skip to content

Instantly share code, notes, and snippets.

@kitt1987
Created June 3, 2016 06:24
Show Gist options
  • Save kitt1987/7dfcd1ab87e31f667f7078db710bb766 to your computer and use it in GitHub Desktop.
Save kitt1987/7dfcd1ab87e31f667f7078db710bb766 to your computer and use it in GitHub Desktop.
Helper for calling and binding function as private method in javascript
class PrivateFuncHelper {
constructor() {
var self = this;
Object.defineProperties(this, {
'_': {
value: function(func) {
return func.apply(self, Array.prototype.slice.call(arguments, 1));
}
},
'_bind': {
value: function(func) {
var args = Array.prototype.slice.call(arguments, 1);
return function() {
return func.apply(
self,
Array.prototype.concat(args, Array.from(arguments))
);
};
}
}
});
}
}
function privateMethod(args) {
}
class ClassWithPrivateMethod extends PrivateFuncHelper {
publicMethod() {
this._(privateMethod, args);
var x = this._bind(privateMethod, args);
x(otherArgs);
}
}
module.exports = ClassWithPrivateMethod;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment