Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Вопрос про scope (https://javascript.ru/forum/misc/10067-vopros-pro-scope.html)

B~Vladi 19.06.2010 15:14

Всем спасибо, тему можно закрывать. Демо-код получился такой:
(function(){
  if(this == window){
    document.userScope = arguments.callee;
  } else {
    with(this.stack){ // Область видимости для вражеского фронта
      eval('(function(){' + this.code + '}).call(this.module);');
    }
    this.stack[this.name] = this.module;
  }

})();

(function(){ // Мой фронт
  var userScope = document.userScope;
  delete document.userScope;

  var stack = {};

  var module1 = {};
  var module2 = {};

  userScope.call({ // Добавляем первую переменную
    name: 'module1',
    code: 'this.fn = function(){console.log(module2)};', // Вражеский фронт
    module: module1,
    stack: stack
  });

  userScope.call({
    name: 'module2',
    code: 'console.log(this)',
    module: module2,
    stack: stack
  });

  module1.fn(); // После добавления новых переменных, старые их тоже видят.

})();

Ничего лишнего не попало во вражескую область видимости.


Часовой пояс GMT +3, время: 03:01.