2011-12-02 5 views
44

私はRequireJSに関するいくつかのチュートリアルを読み始めました。それらのどれにも、 "定義する"キーワードが私にとって満足に説明されていませんでした。誰かが私に次のことを教えてくれますか:RequireJSライブラリの定義の説明

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

"定義"とは何ですか?配列とその内部に無名関数を持つ関数を定義していますか?それとも別のことですか?誰かが私にこの種の定義に関する情報を与えることができますか?

追加:あなたの答えはnnnnnnとpradeekに感謝します。ヨーロッパでは、質問を投稿していた夜2時30分だった。たぶん私はそれが単純な関数呼び出しであるとは思わなかったでしょう。

答えて

57

defineはRequireJSに固有ではなく、AMD specificationの一部です。バーク氏は、AMDがブラウザを念頭に置いたわけではないため、RequireJSがAMDの仕様を正確に実装していないことに気付くでしょう。

defineには、匿名機能はありません。 defineは、データをロードするためにAMDベースのJavaScriptファイルで利用できる方法です。 RequireJSのようなライブラリはこれをあなたに利用可能にします。特定の実装はおそらく貴重ではありません。モジュールを宣言する最も一般的な方法であるので、私があなたが提供したモジュールを上に行くでしょう。

define([array]object);

アレイは、このモジュールが依存するモジュールのリストです。モジュールとファイルには1対1の関係があります。 1つのファイルに複数のモジュールを持つことも、1つのモジュールに複数のファイルを入れることもできません。

オブジェクトは、定義するモジュールです。これは何でも、構造体、または構造体を返す関数にすることができます。詳細はRequireJSのドキュメントをご覧ください。

objectが関数の場合、関数に渡される引数は、最初のdefine引数に依存関係としてリストされたモジュールです。また、関数をobjectとして渡すときよりも重要なのは、1回だけ実行されることです。この1つのインスタンス化で作成されたメソッドまたはプロパティは、いつでもアクセスできます。このモジュールは、このモジュールを依存関係としてリストする他のモジュールからアクセスできます。

幸運にも、私はこれで遊んで、物事が意味をなさないときにはドキュメントを読むことをお勧めします。 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がrequireの最後に定義されています。jsが(私もこのdefine言葉があるものの種類を思っていたが、これは私が探していた答えです):私はRequireJs API specificationはかなりうまくまとめて考える

/** 
* 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

モジュールに依存性がある場合、最初の引数は依存関係の名前の配列でなければならず、2番目の引数は定義関数でなければなりません。すべての依存関係がロードされたら、関数を呼び出してモジュールを定義します。関数は、モジュールを定義するオブジェクトを返す必要があります。

これらは、定義のさまざまな構文形式のすべての例をリストしています。

関連する問題