Pure JavaScript – Private and public methods

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() {

        MyClass.prototype = {
            init: function() {
                console.log('init ');



        return MyClass;

var mc = new MyClass();

So finally you can access init() but cannot access prvMethod(). It has only access into MyClass() function scope.

Published by Piotr Sikora

Piotr Sikora is Front End Team Manager @ Sourceful. MeetJS Kielce Committee member. JavaScript enthusiast. With passion to MTB (http://ckbikers.com)

Leave a comment

Your email address will not be published. Required fields are marked *