Skip to content

Instantly share code, notes, and snippets.

@azu
Created November 13, 2012 14:02
Show Gist options
  • Save azu/4065899 to your computer and use it in GitHub Desktop.
Save azu/4065899 to your computer and use it in GitHub Desktop.
function Sandbox(){
    // 必要なモジュールの初期化
    for (var i = 0,len = modules.length; i < len; i++) {
        Sandbox.modules[modules[i]](this);
    }
}

ここでのthisはSandboxのインスタンスなので、

Sandbox("event", function (box){

});

なら、

Sandbox.modules["event"](this);

という呼び出しがされて、

// box === Sandboxのインスタンス
Sandbox.modules.event = function(box){
    box.attachEvent = function(){
    }
    box.dettachEvent = function(){
    }
};

は、要thisにattachEvent等の拡張をしてる。

最後のcallback(this);の時点でのthisは、 Sandbox.modules.event とかで attachEventdettachEvent がついてたり、 Sandbox.prototypeでプロトタイプ拡張したメソッドやプロパティがついてるので、 コールバックに渡すboxは拡張してくっつけたメソッドやプロパティが呼べるという感じになる。

function (box){
    // Sandbox.modulesで拡張したもの
    box.attachEvent(...);
    box.dettachEvent(...);
    // Sandbox.prototypeで拡張したもの
    box.name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment