JavaScript Kit > JavaScript Reference > Here
Function
Functions in JavaScript let you define code that is called on demand, instead of immediately. There are several ways to define a function:
Standard function Statement:
function getarea(w,h){ //standard function
var area=w*h
return area
}
getarea(3,5) //calls function
Starting in JavaScript 1.5, you can define a function conditionally inside an if
clause:
if (calculateit == true){
function getarea(w,h){ //standard function
var area=w*h
return area
}
}
Function Literal (an anonymous function assigned to a variable):
var getarea=function(w,h){
var area=w*h
return area
}
getarea(3,5) //calls function
Function Constructor (creates a function on the fly, which is slower and generally discouraged):
//syntax: new Function(argument1, argument2, ..., argumentY, functionbody) //all parameters must be a string
var getarea=new Function("w", "h", "var area=w*h; return area")
getarea(3,5) //calls function
Related Tutorials
- Functions and creating your own functions
- Robust functions via the arguments array
- Using named arguments in JavaScript functions
Properties
Properties | Description | ||||||
---|---|---|---|---|---|---|---|
__defineGetter__
JavaScript 1.5 feature, supported in FF, Chrome, Safari 3+, but not IE10 |
Method for getting the value of a property of a JavaScript object or function. In the past coders have defined their own getter and setter functions within an object to conveniently access and change data within an object- now you can do this natively and using more streamlined code.
The following defines two getter functions inside another to access the "private" properties of the later: function getarea(width, height){ When defining a getter method inside an object literal, the syntax is different, and should be in the form var myspace = { |
||||||
__defineSetter__
JavaScript 1.5 feature, supported in FF, Chrome, Safari 3+, but not IE10 |
Method for setting the value of a property of a JavaScript object or function. Lets extend the getarea() function above with a setter method for the private variable "w":
function getarea(width, height){ As you can see, when defining a setter method, the syntax is almost identical to a getter method, with the exception that its function accepts a parameter whose value is assigned to it by you when calling it. Getter/ Setter methods can also be defined on the prototype property of objects. The following defines a setter method on the Array object to modify the order of the array when set: Array.prototype.__defineSetter__("sorttype", function(x){ When defining a setter method inside an object literal, the syntax is different, and should be in the form var myobject = { |
||||||
arguments |
A local variable that points to the "arguments " object, which contains all of the arguments passed into the function. Use "arguments.length " to determine the number of arguments entered. You can use this property to define functions that accept varying number of parameters. The following function returns the sum of all of the parameters' values entered into it:
function sumof(){ The
|
||||||
caller |
References the function in which the current function is called inside of. If the current function is called at the top level, caller returns null. You can use this property to check the context in which a function is being called. For example:
function calltest(){ |
||||||
length |
Returns the number of expected parameters for a function. Contrast that with arguments.length , which returns the actual number of parameters entered. The following function contains a generic check to ensure the user has entered the expected number of parameters into it:
function trianglearea(b,h){ |
||||||
prototype |
Lets you add custom properties and methods to a function's prototype object, which instantly adds them to all instances of the function initialized using the new operator (also called a constructor function). In the below, I'm adding a getprice() method to the constructor function Car() that is reflected on all instances of new Car() :
function Car(baseprice, years_old){ You can also use the prototype object to extend prebuilt JavaScript objects that are initialized using the String.prototype.backwards=function(){ |
||||||
Methods
- JavaScript Operators
- JavaScript Statements
- Global functions
- JavaScript Events
- Escape Sequences
- Reserved Words