Variable Types in JavaScript


JavaScript is often characterized as having no data types. Everything is just an object. This is basically true; however, some primitive types can be assigned to variables, and the variables can pick up those types (this is similar to variants for those of you with COM experience). Numbers, strings, Booleans, and functions are the primitive types in JavaScript. They are the building blocks for all objects in JavaScript. Even the higher order “types” that are predefined like arrays and regular expressions are just collections of the basic types.

In Listing 3-3 (boolean.aspx), I have declared a few variables, assigned them integer values, and then displayed true and false after treating them as Boolean values. The key thing to note is that JavaScript follows the C-style convention of treating 0 as false and everything else as true. When the page is run, the result is to display ‘true’, ‘false’, and ‘true’ for one, zero, and negative one.

Listing 3-3

image from book
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">     <title>Booleans</title>     <script type="text/javascript">     var myOne = 1;     var myZero = 0;     var myNegativeOne = -1;     var myTrue = true;     var myFalse = false;     if(myOne) {         alert(true);     }     if(!myZero) {         alert(false);     }     if(myNegativeOne) {         alert(true);     }     </script> </head> <body>     <form  runat="server">     <div>     </div>     </form> </body> </html> 
image from book

To the .NET developer, this set of basic types is pretty straightforward. Strings, numbers, and Booleans are fundamental to all programming tasks, so having them as basic types seems natural. But you can’t anchor a variable to specific data types when they are declared in JavaScript, so you have to use greater care when writing code. You won’t get an error if you assign a string to a number variable, since the variable isn’t locked down to holding only numbers. Still, this can generate an odd error at runtime when you try to access that variable as a number. This is a common problem of all typeless interpreted languages.

Without the ability to tie variables to specific types, you lack static type safety. The language is not compiled; it is interpreted. Types can be created at runtime and dynamically altered during script execution. In other languages, you must declare a type for a variable, and if the variable is used to refer to an object of a different type, you will get a compilation error. Another option in C++ and .NET languages is to carefully coerce variables from one type to another. This is not the case in JavaScript. There is only one type of variable declaration, and no type specifier is permitted. Any variable can refer to any kind of object at any time. In Listing 3-4 (from variablesTypes.aspx), a single variable is declared and then assigned to the fundamental types used in the previous example. I use the typeof operator to show that the type reference changes while the script is being executed.

Listing 3-4

image from book
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head>     <title>Code Test Page</title> </head> <body> <script type="text/javascript"> var variable; alert(typeof(variable));//refers to something undefined; variable = 1;// refers to a number alert(typeof(variable)); variable = true; //refers to a Boolean alert(typeof(variable)); variable = "a string"; //refers to a string alert(typeof(variable)); </script> </body> </html> 
image from book

JavaScript does not consider this to be an error, because this is how it was designed to work (but this particular code snippet may not be a wise way to write your code, of course). Any variable can point to any object at any time. At first glance, this lack of type checking may not seem important, but it is a key element to the power that JavaScript can bring to your code. It is also central to some of the complications you will encounter when programming with JavaScript.

The Microsoft AJAX Library takes advantage of the extensible nature of JavaScript to extend its functionality to provide a more familiar object-oriented approach to development, even when using a typeless language, by layering some additional standard type treatment onto the language. Microsoft couldn’t modify the underlying typeless nature of the language, but they were able to provide a better way of using it that will allow you to represent types in some ways that help to mitigate the underlying limitations. In addition to the basic building block types, two other types are central to programming in JavaScript. Anything that is not a number, Boolean, or string is either a function or an object. A function in JavaScript is a first-class type. It can be passed as an argument to another object. A function can receive arbitrary arguments to act on. It can return primitive types, or it can even return a new function. Listing 3-5 (CreateFunction.aspx) presents an example that is admittedly not very useful in the real world but will illustrate this point. The CreateFunction function actually creates and returns one of two new functions that are stored and then invoked by calling CreateFunction. By calling CreateFunction you produce a function to either double or triple numbers. In reality, you would just use the built-in math libraries for this sort of thing.

Listing 3-5

image from book
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head>     <title>Code Test Page</title> </head> <body> <script type="text/javascript"> function CreateFunction(s) {     if (s ===2) {         return function (s) { return s*2; }         }     else if( s=== 3) {         return function(s) { return s*3; }     } } var DoubleIt = CreateFunction(2); var TripleIt = CreateFunction(3); alert(DoubleIt(2)); alert(TripleIt(4)); alert(typeof(CreateFunction)); alert(typeof(DoubleIt)); </script> </body> </html> 
image from book




Professional ASP. NET 2.0 AJAX
Professional ASP.NET 2.0 AJAX (Programmer to Programmer)
ISBN: 0470109629
EAN: 2147483647
Year: 2004
Pages: 107

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