Function
From PlainOldWebserver
Function()
new Function() is rarely used, since writing function() is so easy. Function has some interesting properties. Here, Function.call() is used to redefine 'this' on the fly. Notice that log_status is passed around to wherever class wants to use it.
function log_status(status) {
document.writeln(" status is " + status );
document.writeln(" Bear is "+this.teddy_bear);
}
function Class1(init_status) {
this.teddy_bear = "armed";
log_status.call(this, init_status);
}
function Class2(init_status) {
this.teddy_bear = "ready";
log_status.call(this, init_status);
}
var a = new Class1("GOOD");
var b = new Class2("READY");
Result:
status is GOOD Bear is armed status is READY Bear is ready