04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3: Language Basics<br />

It’s recommended that a function either always return a value or never return a value. Writing a<br />

function that sometimes returns a value causes confusion, especially during debugging.<br />

Understanding Arguments<br />

Function arguments in ECMAScript don ’ t behave in the same way as function arguments in most other<br />

languages. An ECMAScript function doesn ’ t care how many arguments are passed in, nor does it care<br />

about the data types of those arguments. Just because you define a function to accept two arguments<br />

doesn ’ t mean you can pass in only two arguments. You could pass in one or three or none, and the<br />

interpreter won ’ t complain. This happens because arguments in ECMAScript are represented as an array<br />

internally. The array is always passed to the function, but the function doesn ’ t care what (if anything) is<br />

in the array. If the array arrives with zero items, that ’ s fine; if it arrives with more, that ’ s okay too. In fact,<br />

there actually is an arguments object that can be accessed while inside a function to retrieve the values<br />

of each argument that was passed in.<br />

The arguments object acts like an array (though it isn ’ t an instance of Array ) in that you can access each<br />

argument using bracket notation (the first argument is arguments[0] , the second is arguments[1] ,<br />

and so on) and determine how many arguments were passed in by using the length property. In the<br />

previous example, the sayHi() function ’ s first argument is named name . The same value can be<br />

accessed by referencing arguments[0] . Therefore, the function can be rewritten without naming the<br />

arguments explicitly, like this:<br />

function sayHi() {<br />

alert(“Hello “ + arguments[0] + “,” + arguments[1]);<br />

}<br />

In this rewritten version of the function, there are no named arguments. The name and message<br />

arguments have been removed, yet the function will behave appropriately. This illustrates an important<br />

point about functions in ECMAScript: named arguments are a convenience, not a necessity. Unlike in<br />

other languages, naming your arguments in ECMAScript does not create a function signature that must<br />

be matched later on; there is no validation against named arguments.<br />

The arguments object can also be used to check the number of arguments passed into the function via<br />

the length property. The following example outputs the number of arguments passed into the function<br />

each time it is called:<br />

function howManyArgs() {<br />

alert(arguments.length);<br />

}<br />

howManyArgs(“string”, 45); //2<br />

howManyArgs(); //0<br />

howManyArgs(12); //1<br />

74

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!