var currentScope = {}; var currentScopeStack = [currentScope]; currentScope.a = 0; currentScope.b = 0; function findScopeWithProperty(stack, property) { return stack.concat().reverse().find(e => e.hasOwnProperty(property)); } (function(currentScopeStack) { var currentScope = {}; currentScopeStack.push(currentScope); currentScope.a = 1; console.log(findScopeWithProperty(currentScopeStack, "a").a); // 1 console.log(findScopeWithProperty(currentScopeStack, "b").b); // 0 findScopeWithProperty(currentScopeStack, "b").b = 10; (function(currentScopeStack) { var currentScope = {}; currentScopeStack.push(currentScope); currentScope.b = 2; console.log(findScopeWithProperty(currentScopeStack, "a").a); // 1 console.log(findScopeWithProperty(currentScopeStack, "b").b); // 2 })(currentScopeStack.concat()) })(currentScopeStack.concat()) console.log(findScopeWithProperty(currentScopeStack, "a").a) // 0 console.log(findScopeWithProperty(currentScopeStack, "b").b) // 10