Unlock the Secrets: Mastering Private and Public Methods in Pure JavaScript
Read Time:20 Second
Have you been creating your own classes in pure JavaScript?
What if you neeed a private method? How to create them?
var MyClass = (function() {
function MyClass() {
}
var prvMethod = function() {
console.log('method');
};
MyClass.prototype = {
init: function() {
console.log('init ');
prvMethod();
}
};
return MyClass;
})();
var mc = new MyClass();
mc.init();
So finally you can access init() but cannot access prvMethod(). It has only access into MyClass() function scope.