2012-04-06 69 views
0

我在新的JavaScript,我必須瞭解的Appcelerator的項目。在appcelerator中,我們需要用java腳本編寫代碼。無法理解這段JavaScript代碼

var winForm = (function() { 

    var API = {}; 

    API.list = [ 
     { 
      title:'title1', hasChild:true, color:'#9B0B0B',font:'font' 
     }, 
     { 
      title:'title2', hasChild:true, color:'#9B0B0B',font:'font' 
     }, 
     { 
      title:'title3', hasChild:true, color:'#9B0B0B',font:'font' 
     } 
    ];//end winList 

    return API; 
})(); //end 
module.exports = winForm; 
  1. 什麼類型的功能,這是?
  2. 這是什麼 '{}' 初始化?
  3. 我們在API.list在做什麼?
  4. 如何調用這個函數和列表。
  5. 這是什麼出口?

對不起,問了這麼多問題,在一個職位。

回答

2
  1. 這是一個匿名函數,聲明創建範圍。
  2. 它創建一個空對象。它沒有任何屬性,但它已被定義。
  3. list正被設置爲由3個對象組成的數組(即[ ... ]表示法)。每個對象都具有4個屬性,titlehasChildcolor,和font,與相應的值。
  4. 你不能明確地調用這個函數,它被聲明,運行,並且結果存儲在變量winForm中(然後存儲在module.exports中)。
  5. module對象,不管它是什麼的某些屬性。

您應該花時間瞭解更多關於javascript如何工作的信息。我建議http://javascript.info/作爲一個偉大的增長。

1

1)的功能是在variable = (function() {}())

2)API的形式立即匿名自我執行表達式函數被初始化爲一個對象(或哈希表),其範圍是該函數內部

3 )API.list是對象的一個​​數組,每一個含有4對key:value

4)功能是自執行,從而當返回API對象你把它分配給WINFORM可變

5)winForm是返回對象,winForm.list是數組。
由於分配module.exports = winForm;然後module.exports.list是你的陣列

0
1.This function is called as anonymous function or rather you can say self executing function 

2.It is creating an empty object 

3.API.list is array of the object .. To define array [ ] these brackets are used and for object { }. 
4. You are using the return function .. and the result is getting stored in module.export 
5. Export is the method name .. There has to be a method object define somewhere in js . you can you this method to get your result 
as in the winForm function and used for some purpose