This code scares me. It should not be fully used. (Unless there really is a convincing excuse, I don't know a single 1 2. ) In addition, there is no “functional programming” in the code above, except possibly using a callback. But hell, even C can trivially do this, since there was no closure.
_
is only a valid identifier (e.g. foo
or _foo
or $0
). It doesn’t care (the value matches the entire agreed text, since the function is a “callback” to match RegExp).
$0
and $1
are replaced by arguments[0]
and arguments[1]
respectively (remember that this is a text substitution of the "string" value!). It could be printed manually without a "macro":
function () { return arguments[0] + arguments[1] }
Or what I would do:
function (a,b) { return a + b }
The rest ( Function.toString
and eval(functionStr)
) are meaningless to support macro photography, which is based on: Function -> codeString -> alteredCodeString -> Function
.
This code requires a working Function.toString
, which can emit a "source as is". I'm not sure when IE started supporting this, but it seems to work in IE9.
1 This is a bit of a white lie, but the only place I used this approach is IE in the sidebar checkmark, where the DOM can be “prematurely unloaded” when the popup has been removed, and some other mess with trying to call a function in a different context window
. All about a dirty situation.
2 The JavaScript function library shows how many macros can be made without eval
.
user166390
source share