Wednesday, March 2, 2011

Tips and Tricks in javascript

Source : http://www.ajaxwith.com/Use-Arguments-Define-Functions-JavaScript.html

Use Arguments to Define Functions in JavaScript


Functions are extended or limited in an Ajax and JavaScript based applications through parameters. With the aid of this small tool, developers can customize their functions so that it could properly interpret different types of information and/or commands. It is also very straightforward which also aids in client side interpretation.

But parameters are not a requirement in an Ajax and JavaScript functions. Instead of using parameters, it is also possible for developers to use arguments. This is a small trick in JavaScript and Ajax which could be used in certain situations which ultimately eases web development. It is a bit tricky but works without any problem in any browser. You do not have to push any definitions for your parameters – you can allow arguments to do the same task.

Understanding Parameters

Before continuing with the arguments, first understand the role of parameters. Parameters are practically a “limiter” or a tool that provides a different interpretation. A definition is a good addition to parameters so that it can be properly identified. The identified is implemented within () so that it can be invoked when the parameter is used.

Instead of using a definition within the parenthesis of a parameter, you can use an argument. The use of argument in JavaScript is not unique compared to other programming language. However, the idea of “array is not an array” could be attributed to the argument. Here is an example:

function enterName(a,b,c,d){
     console.log(arguments)
}

The arguments added in this example help define the functions entered. This is where you can see the other use of arguments – arguments define the data entered in the function which is practically the same role required in the parameter.

When you combine a parameter and an argument, you will get something like this:

function(//variable with parameters// //argument function//; operation)

It is a straightforward method of adding argument or defining the parameters. Notice in the end that you are adding an operation. The argument will require an operation which interprets the data entered in the function.

Small Downsides

Although this feature on arguments is quite impressive, it has its shortcomings which should be considered by any developer:

Not a JavaScript array – As already indicated, arguments are perfect examples of an “array is not an array”. This type of implementation is not a JavaScript array. When implemented in this manner, the argument becomes another object. This means it cannot use other functions related to arrays.

Referencing not Interpreting – This can easily slow down an application. Although this form of coding is accepted in major browsers, the arguments will not be considered essential. This means the arguments will only become a reference and this could slow down the rest of the functions attached to the arguments.

If you are planning to build a small Ajax and JavaScript application, this trick can work to your advantage. Simply add the arguments to the variables so that the parameters can be read with the arguments. The only downside is that the arguments become objects and it could slow down data intensive transactions. It is a simple trick that can be useful in some online applications.

No comments:

Post a Comment