Section A.1. Chapter 2


A.1. Chapter 2

  1. The following are valid:

  2. $someVariable _someVariable some_variable somèvariable

  3. The function variable uses a reserved JavaScript keyword, someVariable and 1Variable both start with invalid characters, and some*variable uses an invalid character, a JavaScript operator (*), as part of the variable name. All invalid names generate JavaScript errors.

  4. The identifiers are converted as follows:

  5. The variable some-month becomes someMonth, using CamelCase notation.

  6. The function theMonth becomes getTheCurrentMonth, using relevant verbs and other distinguishing information.

  7. The const current-month becomes CURRENT-MONTH, using constant-width uppercase letters.

  8. The variable summer-month becomes summerMonths, maintaining consistency between the array of items and variable name.

  9. The MyLibrary-afunction function becomes mylibraryFunctionverbFunctionname.

  10. Important point to remember: conjugation is the bane of coders. Use a backslash before the inner single quote so that itâ??s interpreted literally, and not end-of-string:

  11. var someString = â??Who once said, â??Only two things are infinite, the universe and human stupidity, and I\â??m not sure about the former.â?â??;

  12. The following code would work:

  13. var fltNumber = 432.54; var intNumber = parseInt(fltNumber); var octNumber = parseInt(intNumber,2); var hexNumber = parseInt(intNumber,16);

  14. The function parseInt returns the decimal base integer of the floating-point number, which is 432. It can also take a second parameter, specifying base: 8 for octal and 16 for hexidecimal.

  15. Ah, trick question here. Passing a variable thatâ??s not been declared to a function, user function, or JavaScript function results in a JavaScript error, so your function code will never need to test for this.

  16. To test to see whether the value has been set, use a conditional statement:

  17. function test(a) {  if (a) {  // some code  } }




Learning JavaScript
Learning JavaScript, 2nd Edition
ISBN: 0596521871
EAN: 2147483647
Year: 2006
Pages: 151

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net