If I have a function like this:
function showAlert() {
alert("1");
}
Is there a way to bind additional functionality to the same function in a different part of the page?
Like this:
function showAlert() {
alert("1");
alert("2");
}
If I have a function like this:
function showAlert() {
alert("1");
}
Is there a way to bind additional functionality to the same function in a different part of the page?
Like this:
function showAlert() {
alert("1");
alert("2");
}
You can overwrite the function with a new function that calls the old function.
function showAlert() {
alert("1");
}
showAlert = (function (original){
return function () {
original();
alert("2");
}
}(showAlert));
showAlert();
Well, you can simply redefine the function:
function showAlert() {
alert("1");
}
var originalShowAlert = showAlert;
showAlert = function(){
originalShowAlert();
alert("2");
}
showAlert();
Since functions are treated simply as objects in JS, you can maintain an array of functions, then execute each function in a loop. This allows you to add any type of function that you want to the collection (and even remove them later if you keep the index), without breaking the execution train.
var funk = [];
funk.push(function showAlert1()
{
alert("1");
});
funk.push(function showAlert2()
{
alert("2");
});
funk.push(function showAlertFromMsg()
{
alert("You passed " + this);
});
for(var i=0,len=funk.length; i<len; i++) funk[i].call("This is my message");