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

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about singleton