Monday, October 4, 2010 | By: mayurJavascript

Function in javascript

he JavaScript function is used to contain a set of commands that can be called upon by the HTML coding you specify within the BODY of your document. Any of the many JavaScript commands may be placed within the function, in any amount and in any degree of complexity which you have a use for. As well, you may specify any number of individual functions within your document. While the JavaScript function is not as powerful as those found in languages like C and C++, there is still enough functionality to achieve most actions the modern web page requires.
As with all JavaScript commands, the JavaScript function must be placed within the opening and closing HTML SCRIPT tags, as the following syntax example shows. 

<SCRIPT LANGUAGE="JavaScript">

function functionName(optional parameters) {
   statements;
   }

</SCRIPT> 
You can see that the function is fairly straightforward and is relatively easy to use. Calling a function within your document is as easy as simply stating the functionName within an event handler, as the following syntax example shows.
onClick="functionName(optional parameters) ; return true" 
 
Any event handler that applies to the HTML element it is being used with may call the JavaScript function. The "return true" is used to return a value of true to the script, although some scripts may require a "return false" statement, which returns a value of false to the script. Both are acceptable, and the decisions to use both of them are on a case-by-case basis, according to the demands of the script.
All JavaScript functions use the "call by value" method of using arguments, which basically just makes a copy of the argument. The argument is then used for whatever task you've specified and discarded, if not again required by the script. The original argument is not altered in any way. This "call by value" method is the opposite of the "call by reference" method, which alters the original argument directly without making a copy. When the script is finished execution on the argument, the original has been changed. This can lead to problems at a later time, making the script unduly complicated. Because of this, JavaScript does not use the "call by reference" method.

Returning Values to the JavaScript Function

On occasion, you'll have need to return a value to the function you are working with. To do this, you would use the "return" keyword. The value to be returned to the function is simply stated after the return keyword, anywhere within the function. If no value is specified after the return keyword, a value of "undefined" is returned to the function (this is good to know, as some of your JavaScript machinations may require an initial "undefined" value to be returned). This value returned may then be assigned to a variable for later use or used within an expression, as the following syntax example shows. 

<SCRIPT LANGUAGE="JavaScript">

function functionName(optional parameters) {
   statements;
   return variableName = value;
   }

</SCRIPT> 
 
You can see that the return keyword has been used within the function to create a variable with a value and return it to the function. Any statements within that function can then use the variable, as the variable is of the Local type. The variable is not available to statements or functions outside of the function it was created in. For a more detailed discussion of Local versus Global Variables, see the JavaScript Tutorial on Variables.

The Function as an Object

While most instances of creating a function will involve the function being created as the document loads into the browser, there is another method that only creates the function when it is called upon by other parts of the script. In using this method, the function is considered to be an object, and is treated as such by the script. Examine the below syntax example of creating a function as an object.
var variableName = new Function(argumentOne, argumentTwo, etc.); 
When stating a new instance of the core JavaScript Function object as a variable, the function is treated as an object. The capabilities of the function as an object are less than the other more common method of creating a function. There is no space provided for a statement to be executed on what would be the parameters of the common method of creating a function. There are only a set of arguments available to achieve what it is you set out to do. Use this method when you want to perform simple machinations on a variable that has been created earlier within the script.

0 comments:

Post a Comment