Changing the context of a self-executing function
Posted
by
TaylorMac
on Stack Overflow
See other posts from Stack Overflow
or by TaylorMac
Published on 2012-10-12T03:30:30Z
Indexed on
2012/10/12
3:37 UTC
Read the original article
Hit count: 120
JavaScript
|singleton
This code is copied directly from:
// 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:
It does not return the message. What am I missing?
© Stack Overflow or respective owner