含まれているスクリプトがコンパイルされ、新しいコンテキストで実行される、単純な「require」メカニズム(https://gist.github.com/1031869 )を作成しました。ただし、インクルードされたスクリプトで関数を呼び出して渡すとthis
、インクルードされたスクリプトにプロパティが表示されません。
//required.js - compiled and run in new context
exports.logThis = function(what){
for (key in what) log(key + ' : ' + what[key]);
}
//main.js
logger = require('required');
this.someProp = {some: 'prop'}
logger.logThis({one: 'two'}); //works, prints 'one : two'
logger.logThis(this); //doesn't work, prints nothing. expected 'some : prop'
logger.logThis(this.someProp); //works, prints 'some : prop'