From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/ja/code_snippets/modules/index.html | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 files/ja/code_snippets/modules/index.html (limited to 'files/ja/code_snippets/modules') diff --git a/files/ja/code_snippets/modules/index.html b/files/ja/code_snippets/modules/index.html new file mode 100644 index 0000000000..46b808913a --- /dev/null +++ b/files/ja/code_snippets/modules/index.html @@ -0,0 +1,32 @@ +--- +title: モジュール +slug: Code_snippets/Modules +translation_of: Archive/Add-ons/Code_snippets/Modules +--- +

単純なコードで JavaScript モジュール を Mozilla 固有でないコードにします (ブラウザにポーティングする場合など)。eval() は、ユーザの入力に依存しない EXPORTED_SYMBOLS 配列上でのみ使用されるため心配いりません。

+
function importModule (thatObj) {
+    thatObj = thatObj || window;
+
+    var EXPORTED_SYMBOLS = [
+    // シンボルをここに置く
+    ];
+
+    // ここにコードを書く...
+
+    // コードの終わりに: ('i' や 'thatObj' はエクスポートされません!)
+    for (var i=0; i < EXPORTED_SYMBOLS.length; i++) {thatObj[EXPORTED_SYMBOLS[i]] = eval(EXPORTED_SYMBOLS[i]);}
+}
+
+

あるいは、モジュールを一度だけ使用する場合:

+
(function (thatObj) {
+    thatObj = thatObj || window;
+
+    var EXPORTED_SYMBOLS = [
+    // シンボルをここに置く
+    ];
+
+    // ここにコードを書く...
+
+    // コードの終わりに: ('i' や 'thatObj' はエクスポートされません!)
+    for (var i=0; i < EXPORTED_SYMBOLS.length; i++) {thatObj[EXPORTED_SYMBOLS[i]] = eval(EXPORTED_SYMBOLS[i]);}
+})(); // オブジェクトの引数をここに置けます
-- cgit v1.2.3-54-g00ecf