Here is a tutorial example script showing some of the those features:
// Function_Object_Type.html
// Copyright (c) 2008 by Dr. Herong Yang, http://www.herongyang.com/
// Creating a new function with the "Function()" constructor
var sign = new Function("name",
"this.signature = name;"
+ "sign.count++;"
)
// Adding an instance property to the function
sign.count = 0;
// Checking this object
println("\nAbout this \"sign\":");
println(" Type = "+(typeof sign));
println(" Instance Of Object: "+(sign instanceof Object));
println(" Instance Of Function: "+(sign instanceof Function));
// Executing it on the current object
sign("Herong");
// Calling it on another object
var pc = new Object();
sign.call(pc, "IBM");
// Checking the signature
println("\nExecution result:");
println(" this.signature = " + this.signature);
println(" pc.signature = " + pc.signature);
println(" # of calls = " + sign.count);
If you run this script with "jrunscript", you will get:
About this "sign":
Type = function
Instance Of Object: true
Instance Of Function: true
Execution result:
this.signature = Herong
pc.signature = IBM
# of calls = 2
Now we know how to create functions with the "Function()" constructor.
Read other "Function" related chapters to see more tutorial examples.