I am creating a script that can be used on various websites. Since I do not know the circumstances of its use, I would like to place it as a sandbox, where it does not affect other javascripts on the page and, in turn, is not executed by other javascripts.
The easiest start is to use the self-start function:
(function(){ x = function(){ alert('hi!'); x(); })();
But my problem is that if x is already assigned as such, you cannot override it:
x = function(){ alert('too late!'); (function(){ x = function(){ alert('hi!'); x(); })();
This will warn "too late!" and not βhello!β, which is not my desired effect. Can anyone help?
source share