Changing the context of a self-executing function
- by TaylorMac
This code is copied directly from:
http://www.bennadel.com/blog/2264-Changing-The-Execution-Context-Of-Your-Self-Executing-Function-Blocks-In-JavaScript.htm
// Set the singleton value to the return value of the self-
// executing function block.
var singleton = (function(){
// Declare a private variable.
var message = "Stop playing with your context!";
this.getMessage = function(){
return( message );
};
// Return this object reference.
return( this );
}).call( {} );
// alert the singleton message.
alert( "Message:", singleton.getMessage());
?My thought is that I can use this to better contain the variables and functions in my programs.
However, when I try to run the code in a JSfiddle:
http://jsfiddle.net/xSKHh/
It does not return the message. What am I missing?