Created
June 3, 2016 06:24
-
-
Save kitt1987/7dfcd1ab87e31f667f7078db710bb766 to your computer and use it in GitHub Desktop.
Helper for calling and binding function as private method in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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