简体   繁体   中英

call private function in the constructor of module pattern

This is my first attempt to write a module in java script.

myClass = (function() {
    //private members
    SELF = {};
    //constructor
    (function _init()
    {
        SELF.private_member = 10;
    });

    function _print()
    {
        return SELF.private_member;
    }

    return function() {
       _init();
       this.Print = _print;
    };

})();

var obj = new myClass();

I get a error that function _init() is not defined (chrome). How can this be achieved?

The problem in your code is the way that the init function is declared. The below samples will work.

(A) without the brackets:

myClass = (
function() {
    //private members
    SELF = {};

    //constructor
    //this function is executed every time an object is created
    function _init() {
        SELF.private_member = 10;
    }

    function _print() {
        return SELF.private_member;
    }

    return function() {
       _init();
       this.Print = _print;
    };

})();

(B) The second option is to call the _init() function anonymously like this:

myClass = (function() {
    //private members
    SELF = {};

    //constructor
    (function() {
        SELF.private_member = 10;
    })();

    function _print() {
        return SELF.private_member;
    }

    return function() {
       this.Print = _print;
    };
})();

// Better syntax for example B)
myClass = (function() {
    //private members
    SELF = {};

    function _print() {
        return SELF.private_member;
    }

    //constructor; right before the return statement
    //this code is executed ONCE only
    SELF.private_member = 10;

    return function() {
       //this code is exectuted every time an object is created
       this.Print = _print;
    };
})();

I would use the first example, as sample (B) can be a little tricky: The constructor is called instantly (ie before the function _print() is defined), so you have to take care that any functions/variables that the constructor uses are above the constructor function!

With (A) you don't have this problem.

Functional differences :

(A) The constructor function _init() is called every time you create a new object. Technically this is very close to a real constructor as in other object oriented programming languages.

(B) The constructor code is only called once, when you declare the class. Technically this is not a constructor here but actually some class initialization code.

Couple of questions

  1. Why is _init() a constructor? Constructor should be the one which you are returning.
  2. Any particular reason you have grouped (named function expression) the _init() function?

Ignoring the answers what you want to get can be achieved in two ways

myClass = (function() {
    //private members
    SELF = {};
    //constructor
    (function _init()
    {
        SELF.private_member = 10;
    })();

    function _print()
    {
        return SELF.private_member;
    }

    return function() {
       this.Print = _print;
    };

})();

var obj = new myClass();

and

myClass = (function() {
    //private members
    SELF = {};
    //constructor
    function _init()
    {
        SELF.private_member = 10;
    }

    function _print()
    {
        return SELF.private_member;
    }

    return function() {
       _init();
       this.Print = _print;
    };

})();

var obj = new myClass();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM