Last active
August 29, 2015 13:57
-
-
Save heapwolf/9769400 to your computer and use it in GitHub Desktop.
scope chain storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function get(name) { | |
var c = arguments.callee.caller | |
while(c) { | |
if (c[name]) return c[name] | |
c = c.arguments.callee.caller | |
} | |
} | |
function set(name, value) { | |
var p = arguments.callee.caller | |
p.arguments.callee[name] = value | |
} | |
function three() { | |
console.log(get('x')) | |
} | |
function two() { | |
three() | |
} | |
function one() { | |
set('x', 100) | |
two() | |
} | |
one() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.get = function get(name) { | |
var c = arguments.callee.caller | |
while(c) { | |
if (c[name]) return c[name] | |
c = c.arguments.callee.caller | |
} | |
} | |
exports.set = function set(name, value) { | |
var p = arguments.callee.caller | |
p.arguments.callee[name] = value | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment