11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

}<br />

if (addTwo.arguments.length == 2)<br />

alert(x+y);<br />

Of course, this wouldn‘t correct a bad function call like<br />

addTwo(5,true);<br />

which would produce a value of 6, since true would be converted to the integer 1. If we added<br />

type-checking into our function, we could solve this problem, as shown here:<br />

function addTwo(x,y)<br />

{<br />

if (addTwo.arguments.length == 2)<br />

{<br />

}<br />

if ( (typeof(x) != "number") || (typeof(y) !="number") )<br />

else<br />

return;<br />

}<br />

return;<br />

alert(x+y);<br />

As we can see, to create truly reusable functions that will withstand anything thrown at them,<br />

we will have to put in some more effort.<br />

Comment Your Functions Consider putting a comment block before a function indicating the<br />

name of the function, its purpose, the number and type of parameters accepted, any return<br />

values, and any output the function may produce. An example of such a comment block is<br />

shown here:<br />

/*<br />

Function customAlert(message,icon,color,buttontext)<br />

Description: This function creates a custom alert dialog

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

Saved successfully!

Ooh no, something went wrong!