Created
June 30, 2014 07:29
-
-
Save DmitrySoshnikov/b135eb17ff5997e4f1e7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Apply a function in needed environment. | |
* Firefox only, educational purpose only. | |
* by Dmitry Soshnikov <[email protected]> | |
*/ | |
Function.prototype.setEnvironment = function(environment) { | |
with (environment) { | |
return eval(uneval(this)); | |
} | |
}; | |
(function () { | |
return x + y; | |
}) | |
.setEnvironment({x: 10, y: 20}) | |
.call(); // 30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@WebReflection, yeah, explicit
toString
works well in other browsers too. I just made it idiomaticuneval
-eval
sequence, and since theuneval
isn't standardized (and behind the scene probably doestoSource()
rather thantoString()
), mentioned Firefox only.Though, it's not for JS usage. This snippet is only for explaining generic concept, that without an environment function doesn't make sense. But once we provide the environment, the function has already runtime meaning.
Although, probably with this snippet it's possible to fake some private module data. If some function uses private vars of a module, and at the same time calls some
this
-methods, you may facilitate the private vars with your values.