Advantage of parameterless functions in JavaScript

an article added by: Sonja Lande at 05312007


In: Categories » Computers and technology » AJAX » Advantage of parameterless functions in JavaScript

Using Parameterless Functions

Problem You want to take advantage of parameterless functions in JavaScript. Theory JavaScript functions for the most part have parameters. You may think that the previous sentence states the obvious after all, without parameters, what data could be passed to a function? JavaScript has the ability to declare functions that have no parameters, even though the caller of the function has passed parameters to the function. For example, let’s look at an addition function that uses the num1 and num2 parameters.

    Source: /website/ROOT/ajaxrecipes/javascript/parameterlessfunctions.html
    function AddTwoNumbers(num1, num2) {
    return num1 + num2;
 }

The num1 and num2 parameters are added and the result is returned. Solution Another way to write the same function is to drop the parameters and use the arguments object, as follows:

    function AddTwoNumbers() {
    return arguments[0] + arguments[1];
 }

In this modified version of the function, there are no parameters, but the addition is still possible. And if the method is called like this, an addition occurs: AddTwoNumbers( 2, 2); Here the function is passed two parameters, but the function declaration has none. There can be one, two, or ten parameters it does not matter. Maybe one, or two, or ten of all the parameters will be assigned, or maybe none of them will be assigned. This uncertainty poses an additional challenge because you are left wondering if the parameter is assigned or not. For example, consider the following function declaration.

    Source: /website/ROOT/ajaxrecipes/javascript/parameterlessfunctions.html
    function TooManyParametersNotEnoughArguments(  param1, param2, param3) {
    info(  "TooManyParametersNotEnoughArguments", typeof( param3));
    assertEquals( "undefined", typeof(  param3));
 }

The TooManyParametersNotEnoughArguments function expects three parameters, but it is going to be called with only two parameters, as illustrated by the following calling code: TooManyParametersNotEnoughArguments( 1, 2); There is a disjoint between the caller and the function, and the third parameter is left dangling. To see if a parameter is left dangling, you use the typeof operator and test if the result is undefined.

Do not test for a null value because the third parameter is not null; it is undefined. As a general rule of thumb, don’t use null as a way of testing state. null is ambiguous and can mean different things in different contexts. The value of undefined is not ambiguous it clearly indicates that the variable has not been assigned. Another way of testing whether or not the third parameter has been assigned is to see how many arguments have been passed to the function, as illustrated by the following modified TooManyParametersNotEnoughArguments function:

    function TooManyParametersNotEnoughArguments(  param1, param2, param3) {
    info(  "TooManyParametersNotEnoughArguments", typeof( param3));
    assertEquals( "undefined", typeof(  param3));
    assertEquals(  2, arguments.length);
 }

The bold code is an additional test of the argument.length property that verifies the caller of TooManyParametersNotEnoughArguments is passing in only two arguments. The arguments object is defined locally in the context of the function, thus the arguments instance of one function call will not match the arguments call of another function. It’s important to realize that arguments are arrays that you can manipulate. When you implement functions that have no parameters, you should consider the following:

• If you implement a function and it is declared with three parameters, don’t expect the function to be called with more or fewer parameters. Coding with JavaScript is about convention and expectations. Code with more or fewer parameters when a certain number is expected confuses the developer.

• If you are going to accept a varying number of arguments, then declare the function without arguments, meaning that you will use the arguments objects. Somebody who then reads the function understands that the function will accept a varying number of parameters, again fulfilling the expectations requirement when writing JavaScript code.

• If you declare a function without parameters, and you will be using one index of the arguments object throughout the function, consider assigning the index to a variable identifier so that the parameter’s purpose is apparent.

• If you are going to accept a variable number of arguments, remember to generate explicit and verbose errors whenever you expect more or fewer arguments.

legal notice

Our website is not responsible for the information contained by this article. Web-articles is a free articles resource.
Suggestion: If you need fresh, daily updated content for your website, feel free to use our service. Click here for more information.

Useful tools and features

Link to this article from your page    Send this article to you or to a friend
If you like this article (tutorial), please link to it from your web page using the information above.

related articles

1. Understanding the Definition and Philosophy of REST
Understanding the Definition and Philosophy of REST REST is a controversial topic among Web service enthusiasts, because it’s considered to stand for the opposite of what Web services and SOA are trying to achieve. The problem with this thinking is that REST is not in contradiction with the abstract definition of SOA and Web services. REST is in contradiction with technologies such as SOAP, WSDL, and WS-* specifications. The following offers a quick definition of REST:...

2. The Easiest Way to Get Started with Ajax and REST
The Easiest Way to Get Started with Ajax and REST Problem You want to know the best way to get started with writing Ajax and REST. Solution When developing an Ajax and REST application, you must decide on the tools and frameworks you’ll use. The choice is simple: Use whatever you’re using today, and write some Ajax applications. You don’t need to change the tools you’re using today. Whether you’re using ASP.NET, JavaServer Pages (JSP), PHP, Ruby, or Python, you...

3. Testing a Dynamic Contract with Ajax
Coding the Contract Using Test-Driven Development Techniques Coding the contract using agile and test-driven development techniques requires writing a number of tests and implementing aMock URL layer. Problem You want to code the contract using these development techniques. Solution To demonstrate, let’s define a use case, implement the use case as a contract, write a test case(s) to implement the contract, implement the contract in the Mock URL, and finally...

4. Testing the Client Side Logic
Problem You want to effectively test your application’s client-side logic. Theory Testing GUI code tends not to be a productive task because of the complications that arise. The main complication is how to test the correctness of a user interface. Imagine a situation where clicking a button causes a table to be filled with data. Now imagine that when a check box is checked and the button is clicked again, a different table is filled with content. The fact that clicking the same button results in two ...

5. Understanding JavaScript and Types
Understanding JavaScript and Types Problem You want to work around the fact that JavaScript does not have types declared for its variables. Theory JavaScript code does not have any variables with a declared type. The lack of typed variables is apparent when you declare functions. That said, not having typed variable declarations does not mean JavaScript has no types or no type safety. Let’s start out with the simple declaration of a function, as illustrated by the following ex...

6. Coding Using Conventions and Not Configurations
Coding Using Conventions and Not Configurations Problem You want to make your JavaScript constructs more efficient by applying the Rails “convention over configuration” principle to them. Theory You may already be familiar with the programming platform Ruby on Rails, which is used to build Web applications. The focus of this recipe is not Ruby on Rails, but one aspect of Ruby on Rails namely, convention over configuration (see http://en.wikipedia.org/wiki/ Ruby_on_Rails for m...

7. JavaScripot Functions
Treating Functions Like Objects Problem You want to take advantage of the fact that functions are objects (remember, everything is an object in JavaScript). Theory Many people think that a function is some keyword used in JavaScript. A function is also an object that can be manipulated. Knowing that a function is an object makes it very interesting from the perspective of writing JavaScript code, because the code can treat the function like another other object. This mean...