2011-12-02 54 views
44

我開始閱讀關於RequireJS的幾個教程。在他們中沒有一個是爲我解釋令人滿意的「定義」關鍵字。有人可以幫我:RequireJS庫的定義說明

define(
    ["Models/Person", "Utils/random", "jquery"], 
    function (Person, randomUtility, $) {..} 
) 

什麼是「define」?定義一個函數,其中包含一個數組和一個匿名函數?或者是別的什麼?有人能給我更多關於這種定義的信息嗎?

此外:謝謝你nnnnnn和pradeek你的答案。在歐洲,當我發佈問題的那天晚上是2點30分。也許因此我不認爲這是一個簡單的函數調用。

回答

57

define不是特定於RequireJS,它是AMD specification的一部分。 Burke會注意到RequireJS並沒有完全實現AMD如何指定它,因爲AMD並沒有真正關注瀏覽器。

define在其中沒有匿名功能。 define是一種可用於AMD基於JavaScript的文件加載其數據的方法。像RequireJS這樣的庫可以讓你使用它。具體的實施可能對你沒有價值。所以我會回顧一下你提供的,因爲它是聲明模塊的最常見的方式。

define([array]object);

陣列是模塊的列表,該模塊依賴於。模塊和文件之間有1對1的關係。您不能在一個文件中包含多個模塊,也不能在一個模塊中包含多個文件。

對象是您正在定義的模塊。這可以是任何東西,結構或返回結構的函數。請閱讀RequireJS的文檔瞭解更多詳情。

如果object是一個函數,則傳遞給該函數的參數是作爲第一個define參數中的依賴項列出的模塊。當你通過一個函數object時,它也只會運行一次。在這個實例化上創建的方法或屬性可以隨時訪問,然後可以被其他模塊訪問,這些模塊將這個模塊列爲依賴項。

祝你好運,我建議你在這種情況下玩弄,並在事情沒有意義時閱讀文檔。 RequireJS文檔是AMD模塊如何工作的快速入門。

1

我發現此頁面Why AMD?非常有幫助。從這個頁面總結,AMD規範有助於克服「用隱式依賴關係編寫一堆腳本標記,你必須手動排序」問題。在執行所需功能之前加載依賴關係很有用,類似於其他編程語言(如python)中的import。 AMD還防止全球命名空間污染問題。檢查"It is an improvement over the web's current "globals and script tags" because"部分。

5

我發現define定義在需求的底部附近。JS(我也想知道什麼樣的事情這define字,這就是答案一直在尋找):

/** 
* The function that handles definitions of modules. Differs from 
* require() in that a string for the module should be the first argument, 
* and the function to execute after dependencies are loaded should 
* return a value to define the module corresponding to the first argument's 
* name. 
*/ 
define = function (name, deps, callback) { 
    var node, context; 

    //Allow for anonymous modules 
    if (typeof name !== 'string') { 
     //Adjust args appropriately 
     callback = deps; 
     deps = name; 
     name = null; 
    } 

    //This module may not have dependencies 
    if (!isArray(deps)) { 
     callback = deps; 
     deps = null; 
    } 

    //If no name, and callback is a function, then figure out if it a 
    //CommonJS thing with dependencies. 
    if (!deps && isFunction(callback)) { 
     deps = []; 
     //Remove comments from the callback string, 
     //look for require calls, and pull them into the dependencies, 
     //but only if there are function args. 
     if (callback.length) { 
      callback 
       .toString() 
       .replace(commentRegExp, '') 
       .replace(cjsRequireRegExp, function (match, dep) { 
        deps.push(dep); 
       }); 

      //May be a CommonJS thing even without require calls, but still 
      //could use exports, and module. Avoid doing exports and module 
      //work though if it just needs require. 
      //REQUIRES the function to expect the CommonJS variables in the 
      //order listed below. 
      deps = (callback.length === 1 ? ['require'] : ['require', 'exports', 'module']).concat(deps); 
     } 
    } 

    //If in IE 6-8 and hit an anonymous define() call, do the interactive 
    //work. 
    if (useInteractive) { 
     node = currentlyAddingScript || getInteractiveScript(); 
     if (node) { 
      if (!name) { 
       name = node.getAttribute('data-requiremodule'); 
      } 
      context = contexts[node.getAttribute('data-requirecontext')]; 
     } 
    } 

    //Always save off evaluating the def call until the script onload handler. 
    //This allows multiple modules to be in a file without prematurely 
    //tracing dependencies, and allows for anonymous module support, 
    //where the module name is not known until the script onload event 
    //occurs. If no context, use the global queue, and get it processed 
    //in the onscript load callback. 
    (context ? context.defQueue : globalDefQueue).push([name, deps, callback]); 
}; 
0

我覺得RequireJs API specification概括起來相當不錯:

如果模塊具有依賴關係,則第一個參數應該是一個依賴項名稱數組,而第二個參數應該是一個定義函數。一旦加載了所有依賴關係,該函數將被調用來定義模塊。該函數應該返回一個定義該模塊的對象。

他們列出了所有定義的各種語法形式的例子。