簡體   English   中英

無法理解此 javascript 代碼

[英]not able to understand this javascript code

我是 javascript 的新人,我必須了解一個 appcelerator 項目。 在 appcelerator 中,我們需要編寫 java 腳本。

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. 這是什么類型的 function?
  2. 這個“{}”初始化是什么?
  3. 我們在 API.list 中做什么?
  4. 如何調用此 function 並列出。
  5. 這是什么出口?

抱歉在一個帖子里問了這么多問題。

  1. 這是一個匿名的 function 聲明創建 scope。
  2. 它創建一個空的 object。它沒有屬性,但已定義。
  3. list被設置為由 3 個對象組成的數組(即[... ]表示法)。 每個 object 都有 4 個屬性, titlehasChildcolorfont ,具有各自的值。
  4. 您不能顯式調用此 function,它已聲明、運行,結果存儲在變量winForm中(然后存儲在module.exports中)。
  5. module object 上的某些屬性,無論它是什么。

您應該花時間詳細了解 javascript 的工作原理。 我推薦http://javascript.info/作為一個很好的提升。

1) function 是variable = (function() {}())形式的立即匿名自執行表達式 function

2) API 被初始化為一個 object(或哈希表),它的 scope 在 function 里面

3) API.list 是一個對象數組,每個對象包含四對key:value

4) Function 是自執行的,所以當您返回 API object 時,您將其分配給 winForm 變量

5) winForm是返回object, winForm.list是數組。
因為你分配module.exports = winForm; 那么module.exports.list就是你的數組

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM