How to call a public function from a private function in a JavaScript module template?
For example, in the following code
var myModule = (function() { var private1 = function(){
This question has been asked twice before , with a different accepted answer for everyone.
- Save the link to the returned object before returning it, and then use this link to access the public method. See the answer .
- Keep the public method link in the closure and use this to access the public method. See the answer .
Although these solutions work, they are unsatisfactory from the point of view of OOP. To illustrate what I mean, give a concrete implementation of a snowman with each of these solutions and compare them with a simple object literal.
Snowman 1: Save Link to Return Object
var snowman1 = (function(){ var _sayHello = function(){ console.log("Hello, my name is " + public.name()); }; var public = { name: function(){ return "Olaf"}, greet: function(){ _sayHello(); } }; return public; })()
Snowman 2: Save link to public function
var snowman2 = (function(){ var _sayHello = function(){ console.log("Hello, my name is " + name()); }; var name = function(){ return "Olaf"}; var public = { name: name, greet: function(){ _sayHello(); } }; return public; })()
Snowman 3: literal object
var snowman3 = { name: function(){ return "Olaf"}, greet: function(){ console.log("Hello, my name is " + this.name()); } }
We see that the three are identical in functionality and have the same public methods.
If we do a simple redefinition test, however
var snowman = // snowman1, snowman2, or snowman3 snowman.name = function(){ return "Frosty";} snowman.greet(); // Expecting "Hello, my name is Frosty" // but snowman2 says "Hello, my name is Olaf"
we see that # 2 fails.
If we run a prototype override check,
var snowman = {}; snowman.__proto__ = // snowman1, snowman2, or snowman3 snowman.name = function(){ return "Frosty";} snowman.greet(); // Expecting "Hello, my name is Frosty" // but #1 and #2 both reply "Hello, my name is Olaf"
we see that both # 1 and # 2 fail.
This is a really ugly situation. Just because I decided to somehow reorganize my code, the user of the returned object should carefully examine how I implemented everything to find out if it can override my object methods and expect it to work! While opinions differ here, my own opinion is that the correct override behavior is a simple object literal.
So this is the real question:
Is there a way to call an open method from private so that the resulting object acts as an object literal with respect to overriding behavior?