Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
uupaa edited this pageApr 28, 2015 ·36 revisions

WebModule idiom は、どこでも動くJavaScriptコードを記述するためのイディオム(トリックの一種)です。
WebModule idiom を使うと、各プラットフォームにおけるルートオブジェクト(global object)を獲得する事ができます。

WebModule idiom version 2

2014〜2015年は version 2 のスタイルが使われています。

// WebModule idiom version 2(function(global){"use strict";// global["MyModule"].say("hello");})((this||0).self||global);

WebModule idiom version 2 は各プラットフォームにおいて以下のように評価されます。

platformexpression and result
old Node.js((undefined ││ 0).self ││ global)
((0).self ││ global)
((undefined ││ global)
(global)
→ Global Object
new Node.js
NW.js (Node context)
((global ││ 0).self ││ global)
((global).self ││ global)
((undefined ││ global)
(global)
→ Global Object
WebWorkers((WorkerGlobalScope ││ 0).self ││ undefined)
((WorkerGlobalScope).self)
(WorkerGlobalScope.self)
→ Global Object
Browser
NW.js (Browser context)
((window ││ 0).self ││ undefined)
((window).self)
(window.self)
→ Global Object

WebModule idiom version 1

以下は、2013年に使用されていた古い書き方(version 1)です。

// WebModule idiom version 1(function(global){"use strict";// global["MyModule"].say("hello");})(this.self||global);

ES6 and modules

ES6 Module スコープ内はデフォルトで strict モードになります。
また strict モードにおいて this は undefined になるため、WebModule idiom version 2 の((this ││ 0).self ││ global)エラーになる環境も存在します。

(function(global){"use strict";})((this||0).self||global);// -> Error "global" is not defined.

そのような場合はvar global = global || self; を使うと問題を解決可能な場合があります。

varglobal=global||self;(function(global){"use strict";})(global);
Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp