Last active
August 29, 2015 14:05
-
-
Save digitalicarus/f37693972ee71c75b197 to your computer and use it in GitHub Desktop.
Reactive Expressions
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
var EX = function (expr) { | |
var result | |
, operands = {} | |
, operVals = Array.prototype.slice.call(arguments, 1) | |
, operSetFuncs = {} | |
; | |
function genResult () { | |
result = expr.apply(null, Object.keys(operands).map(function(v) { | |
return operands[v]; | |
})); | |
} | |
expr.toString().replace(/^[^(]*\(/g, '').replace(/\).*/g, '').split(/\s*,\s*/).forEach(function(v, i) { | |
var curr = v; | |
operands[curr] = operVals[i]; | |
operSetFuncs[curr] = function (val) { | |
if (val !== undefined) { | |
operands[curr] = val; | |
genResult(); | |
} | |
return operands[curr]; | |
} | |
}); | |
genResult(); | |
return [ | |
function () { | |
return result; | |
}, operSetFuncs | |
]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment