This section provides a tutorial example on how to use an instance properties of a function to share values across multiple function calls.
As an object, a function can also have its own instance properties.
An instance property of a function can also be viewed as a static variable
whose value persists across multiple function calls.
The tutorial example below shows you how to use an instance property of a function
to share values across multiple function calls.
// Function_Own_Properties.js
// Copyright (c) 2008 by Dr. Herong Yang, http://www.herongyang.com/
var sign = new Function("name",
"this.signature = name;"
+ "sign.count++;"
)
sign.count = 0;
// Executing it on the current object
sign("Herong");
// Applying it on another object
var java = new Object();
sign.apply(java, new Array("Sun"));
// Calling it on another object
var pc = new Object();
sign.call(pc, "IBM");
// # of times the function has been called
println("Call counts: " + sign.count);
Run this JavaScript file with "jrunscript" in a command window, you will get: