JavaScript Tutorials - Herong's Tutorial Examples - Version 2.21, by Dr. Herong Yang
Function Object Instance Properties
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) 2013 by Dr. Herong Yang, 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:
Call counts: 3
Table of Contents
ECMAScript Language Specification and JavaScript Dialects
Data Types, Variables and Expressions
Creating, Accessing, and Manipulating Arrays
Defining and Calling Functions
Web Browser Supporting JavaScript
Server-Side and Client-Side Web Scripting
Defining Your Own Object Types
Inheritance of Properties and Methods through the Prototype Object Chain
'jrunscript' - JavaScript Shell Command from JDK
►Using Functions as "Function" Objects
Functions Are Objects of the "Function" Type
Using the Function Constructor
Function Object Inherited Properties and Methods
►Function Object Instance Properties
Creating Function Objects with "function" Statements
Creating Function Objects with the "function" Operator
Comparing 3 Ways of Creating Functions
Introduction to Built-in Object Types