51

RequireJS に関するいくつかのチュートリアルを読み始めました。それらのどれにも、私にとって十分に説明された「define」キーワードはありませんでした。誰かが次のことで私を助けてくれますか:

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

「定義する」とは?配列とその中に無名関数を含む関数を定義していますか? それとも別のものですか?この種の定義について詳しい情報を教えてもらえますか?

追加: nnnnnn と pradeek さん、回答ありがとうございます。ここヨーロッパでは、私が質問を投稿していたのは夜の 2 時 30 分でした。そのため、単純な関数呼び出しだと認識していなかったのかもしれません。

4

4 に答える 4

62

defineRequireJS に固有のものではなく、AMD 仕様の一部です。Burke は、AMD が実際にブラウザーを念頭に置いていなかったため、RequireJS は AMD が指定したとおりに実装されていないことに注意するでしょう。

define匿名関数はありません。 defineデータをロードするために AMD ベースの JavaScript ファイルで使用できるメソッドです。RequireJS のようなライブラリは、これを利用できるようにします。特定の実装は、おそらくあなたにとって価値がありません。モジュールを宣言する最も一般的な方法であるため、提供されたものについて説明します。

define( [array]object );

配列は、このモジュールが依存するモジュールのリストです。モジュールとファイルの間には 1 対 1 の関係があります。1 つのファイルに複数のモジュールを含めることも、1 つのモジュールに複数のファイルを含めることもできません。

オブジェクトは、定義しているモジュールです。これは、構造体、または構造体を返す関数であれば何でもかまいません。詳細については、 RequireJSのドキュメントを参照してください。

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

頑張ってください。これをいじって、意味が分からない場合はドキュメントを読むことをお勧めします。RequireJS のドキュメントは、AMD モジュールがどのように機能するかについてのクイック スタートとして最適です。

于 2011-12-03T02:31:10.327 に答える
5

definerequire.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]);
};
于 2013-11-11T18:52:15.817 に答える
1

このページを見つけましたなぜ AMD なのか? 非常に役立ちます。このページから要約すると、AMD の仕様は、「手動で注文する必要がある暗黙的な依存関係を持つ多数のスクリプト タグを記述する」問題を解決するのに役立ちます。importPython などの他のプログラミング言語と同様に、必要な関数を実行する前に依存関係を読み込むのに役立ちます。AMD は、グローバルな名前空間汚染の問題も防ぎます。セクションを確認してください"It is an improvement over the web's current "globals and script tags" because"

于 2013-08-28T13:18:00.667 に答える
0

RequireJs API 仕様はそれをかなりうまくまとめていると思います。

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

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

于 2015-06-16T00:52:47.057 に答える