Given that I have declared a function in the for of foo = function F() {}
how can I copy but not clone that function.
For example with the code below each time one calls the function log()
with an argument that argument is added to the array log.list
that list can be shown by calling the function log.alert()
.
How can one declare a new variable log2
that will copy the functionality of the log
function but remain independent of it?
i.e an independent list
will be maintained for log
and log2
and by calling log2.alert()
the log2.list
will be shown and by calling log.alert()
the log.list
will be shown?
var log = log || function F (x) {
F.list = F.list || [];
if (x != undefined) F.list.push(x);
if (!F.fired) {
F.alert = function () {
alert (F.list.join("\n"));
}
F.reset = function () {
F.list = [];
}
F.fired = true;
}
}
// Demonstrate the function
log("log #1");
log("log #2");
log("log #3");
log("log #4");
log.alert();