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
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 | ||||||
|---|---|---|---|---|---|---|---|
| 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
| Methods | Description |
|---|---|
| funcA.apply(funcB, [argument1, argument2, argumentN]) | Lets you call a function (funcA) as if it's a method
of another function (funcB). Specifically, the "this"
keyword when used inside funcA now returns funcB. Identical to
call() below, except any arguments passed into funcA should
be wrapped in an array. |
| funcA.call(funcB, argument1, argument2, argumentN) | Lets you call a function (funcA) as if it's a method
of another function (funcB). Specifically, the "this"
keyword when used inside funcA now returns funcB. The two methods
apply() and call() are useful when you
wish an object to "borrow" a method from another object and use it
within its own context.The following illustrates this: function borrowme(name){ Here the custom object "
|
| toString() | Returns a string that represents the source code (raw text) of the function. A useful method in debugging, by looking at the source code of a function. |
- JavaScript Operators
- JavaScript Statements
- Global functions
- JavaScript Events
- Escape Sequences
- Reserved Words
- Ajax (XMLHttpRequest)
- Anchor
- Applet
- Area
- Array
- Boolean
- Date
- Document
- Event
- Form
- Reset
- Frame
- Function
- History
- Image
- Link
- Location
- Math
- Navigator
- Number
- Object
- RegExp
- Screen
- String
- Style
- window
