2012-10-12 67 views
0

此代碼是直接從複製的情況下:更改自動執行功能

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()); 

我的想法是,我可以用它來更好地包含在我的程序的變量和函數。

然而,當我嘗試運行在的jsfiddle代碼:

http://jsfiddle.net/xSKHh/

它不會返回消息。我錯過了什麼?

回答

3

您在通知中缺少加號而不是逗號。 嘗試這樣的:

alert("Message:" + singleton.getMessage()); 
+0

哇好,我覺得我應該重視這個問題了,把一個問題了那裏我實際上有實現這是一個問題......總之,謝謝你 – TaylorMac

+0

護理標記答案被接受? :-) –