Advertisement
Guest User

Untitled

a guest
Nov 9th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var currentScope = {};
  2. var currentScopeStack = [currentScope];
  3.  
  4. currentScope.a = 0;
  5. currentScope.b = 0;
  6.  
  7. function findScopeWithProperty(stack, property) {
  8.     return stack.concat().reverse().find(e => e.hasOwnProperty(property));
  9. }
  10.  
  11. (function(currentScopeStack) {
  12.     var currentScope = {};
  13.     currentScopeStack.push(currentScope);
  14.     currentScope.a = 1;
  15.  
  16.     console.log(findScopeWithProperty(currentScopeStack, "a").a); // 1
  17.     console.log(findScopeWithProperty(currentScopeStack, "b").b); // 0
  18.     findScopeWithProperty(currentScopeStack, "b").b = 10;
  19.    
  20.     (function(currentScopeStack) {
  21.         var currentScope = {};
  22.         currentScopeStack.push(currentScope);
  23.         currentScope.b = 2;
  24.         console.log(findScopeWithProperty(currentScopeStack, "a").a); // 1
  25.         console.log(findScopeWithProperty(currentScopeStack, "b").b); // 2
  26.  
  27.     })(currentScopeStack.concat())
  28. })(currentScopeStack.concat())
  29.  
  30. console.log(findScopeWithProperty(currentScopeStack, "a").a) // 0
  31. console.log(findScopeWithProperty(currentScopeStack, "b").b) // 10
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement