Let\'s say I have window.open (without name parameter), scattered in my project and I want to change the implementation so that wherever name is not specified I\'ll specify
To avoid circular calls, you need to stash away the original window.open
function in a variable.
A nice way (that doesn't pollute the global namespace) is to use a closure. Pass the original window.open
function to an anonymous function as an argument (called open
below). This anonymous function is a factory for your hook function. Your hook function is permanently bound to the original window.open
function via the open
argument:
window.open = function (open) {
return function (url, name, features) {
// set name if missing here
name = name || "default_window_name";
return open.call(window, url, name, features);
};
}(window.open);