2011-05-17 111 views
5

我有以下的javascript:Javascript對象創建最佳實踐

  var MyObject = (function() { 

       function Setup(args) { 
        this.prop1 = args.x; 
        this.prop2 = args.y 
        this.prop3 = this.prop1 + this.prop2; 

        this.Create = function() { 
         return 'a' + helperFunc(); 
        } 

        function helperFunc() { 
         return this.prop3; 
        } 
       } 

       return { 
        init : function(args) { 
         var setup = new Setup(args); 

         setup.Create(); 
        } 
       } 

      })(); 


      $(function() { 

       MyObject.init(someArgs); 

      }); 
  1. 是我的做法提出異議建設一個好的做法呢?

  2. 嘗試訪問this.prop3時,我在幫助功能中獲得undefined

  3. 我也試圖分配this.prop1 + this.prop2到一個局部變量,用函數返回此值,像這樣:

     function Setup(args) { 
           var total; 
    
           this.prop1 = args.x; 
           this.prop2 = args.y 
    
           total = this.prop1 + this.prop2; 
           this.getTotal = function() { 
             return total; 
           }; 
    
           this.prop3 = this.prop1 + this.prop2; 
           ... 
    

...並在helperFunc這樣調用這個時候:

     return this.getTotal(); 

..我得到this.getTotal是不是一個函數

我一直在閱讀周圍的對象創建和使用閉包來模仿私人成員等,因爲沒有一種方法來定義對象,我感到困惑。

TBH - 我真的不明白的結構:

     var myObject = (function() { ... }(); 

我已經看到它使用了很多在jQuery插件但隨後空parenth什麼第一parenth在到底是什麼意思,做?

任何知識的傳授將不勝感激。

另外,我有秩序的道格拉斯Crockford的書JavaScript和直到它到達我需要嘗試解決這個問題

+1

http://stackoverflow.com/questions/3921922/what-does-this-mean-function-x-y-a-b-in-javascript – mplungjan 2011-05-17 12:25:16

+0

這裏有很多問題。如果你試圖限制自己每個問題的一個問題,它會使得提供準確和精確的答案變得容易很多。 – 2011-05-17 12:32:04

+0

mplungjan - 這是理解函數調用結束時的父類的完美鏈接。我只需要了解爲什麼調用函數時調用屬性而不是函數時會出現undefined ... – 2011-05-17 12:40:51

回答

4

Xhalent's wonderful article(真的做得很好,並明確wirtten)提到由他:

那是因爲值「這」 爲「本」,當創建 對象的值不同。

所以你的情況:

... 
    var _this = this.prop3; 
    function helperFunc() { 
    return _this; 
    } 
... 

可能會達到什麼樣的期望的。

+0

這就是爲什麼我得到未定義的答案。我希望我能標記Paul Butchers的回答,因爲這有助於理解問題。我讀過Xhalent的文章,他們非常有用。將該值設置爲一個私有變量我不知道我可以從helperFunc函數訪問它。我花了太多時間在C#上,需要停止思考經典的OOP。期待Douglas Crockford的書。謝謝大家誰回答.... – 2011-05-17 13:32:12

0

您可以通過麥克科斯對OOP在JS上this page有一個很好的教程。

我已經看到它在jQuery中使用了大量 插件,但它隨後空parenth什麼第一 parenth在 到底是什麼意思,做?

第二組parenth立即調用您在第一組parenth中聲明的函數。

你可以宣佈它和separatly執行它(允許多次執行):

var myFunction = function() { alert("boo"); }; // Declare & instanciate 
myFunction(); // Invoke 
myFunction(); // Invoke again 

或者在一行一舉兩得:

(function() { alert("boo"); })(); // Can only invoke now, the function is anonymous and not stored anywhere. 
1

如果您有一個使用this的函數,則必須確保調用上下文正確。即使用this.helperFunc(),而不僅僅是helperFunc()(但您還需要設置以便this.helperFunc被定義)。 this在您的示例中的helperFunc內的引用與Create()中的引用不同。

你可以把它看作雖然功能不是定義爲對象的成員,但稱爲爲對象的成員。

根據具體情況,this可能會解決這三個問題。

  1. 新創建的對象,如果函數調用前面有new關鍵字。
  2. 該函數被調用時點的左側的對象。
  3. 全局對象(window),如果以上都沒有提供。

如果一個函數被調用無一物,如您在this.Create調用helperFunc的情況下,this將被綁定到全局對象(window,在瀏覽器中使用時)

鑑於東西像這樣:

var o = {someVal:"hello"}; 
o.doSomething = function(){alert(this.someVal)}; 

調用o.doSomething()將明顯警報 「你好」。

考慮:

var o2 = {someVal:"world"}; 
o2.someFunc = o.doSomething; 

調用o2.someFunc()將提醒「世界」,而不是「你好」,就好像它是一個指向odoSomething會員,你期望的那樣。

並考慮:

var someFunc = o.doSomething 
someVal = "sailor" 

調用someFunc()會提醒 「水手」。

另一個混亂點是this直接在Setup()內使用。當您使用new調用某個函數時,如您所做的那樣,this未綁定到全局對象,而是綁定到Setup對象的新實例。

對於我上面的示例,這意味着調用new o.doSomething()將警告「未定義」,因爲爲該調用創建的新對象沒有「someVal」成員。