Introduction to JavaScript
with ASP.NET AJAX 1.0
by Ryan Keeter
JavaScript as a dynamic language has many types and a language syntax that is similar to Visual C#. The goal of the ASP .NET AJAX libraries is to create a more object oriented approach to JavaScript. There are ways to create inheritance in JavaScript, and those are called Prototypes. Also, there are many caveats about JavaScript that one must watch out for. These caveats include type coercion that variable scoping.
JavaScript Variables
JavaScript has some first class citizen variables that are very similar to other languages (JavaScript is a C based language after all). For example, the primitive types are :- Number
- String
- Boolean
- Function
Anything other than this, such as arrays are just collections of these basic types. One thing though that one must watch out for when declaring these variables is their scope. Scope is the breadth of which a variable is accessible within an application. JavaScript, as a dynamic language allows for variables to be produced without being declared, and as such, these variables are immediately given a global scope. Furthermore, any variable that is declared within a function is constrained to the scope of that function and cannot be displayed outside of that function. For example:
CODE
function scope()
{
var monkey = "monkey";
}
if (monkey == "monkey")
alert("Monkey doesn't exist, an error will be thrown");
If the keyword “var” is not used to define a variable, then it will have a global scope, even if a variable is defined within a function, the variable is accessible outside of the function.
There are many problems that managed language developers face when dealing with equality in JavaScript. For example, in JavaScript, a string and an integer can be tested for equality and come out true if strict equality is not used. For example:
CODE
if(10 == "10")
alert("This is true");
If the strict equality and inequality operators are used, then type coercion will not happen. To test for equality using strict equality operators, then use “===”, not “==.” For example of a strict inequality testing:
CODE
if(10 !== "10")
alert("Using strict inequality operators will not allow type coercion to happen");
Prototypes
JavaScript allows for a type of object to be created that looks very similar to the objects one might create in such a language like vb script:
CODE
function Person(firstName, lastName)
{
this._firstName = firstName;
this._lastName = lastName;
}
Prototyping is the ability in JavaScript to add a property or a method to a class (such as person). One must define a function, and then call the class, plus the keyword “prototype” then the name of the class inheritance. Here is a code example:
CODE
function Person(firstName, lastName)
{
this._firstName = firstName;
this._lastName = lastName;
}
function fullName()
{
return this._firstName + " " + this._lastName;
}
Person.prototype.FullName = fullName;
var person1 = new Person("Ryan","Keeter");
alert(person1.FullName()); //This will return Ryan Keeter
First the object is defined, then another function is defined (fullName). Using the prototype keyword, a new inherited method is added to the Person object, and it is set to equal the function fullName. When the class is called with the new prototype extender, the function fullName is called also, returning the concatenated first and last names.
Conclusion
Understanding how the JavaScript engine will define, declare, and coerce variables, along with the knowledge of Prototypes, one has the ability to really get creating making some awesome objects and functions.
With this powerful addition to our arsenal, we are primed and ready to see exactly how ASP .NET AJAX, and the Microsoft AJAX libraries are implemented.
This post has been edited by Shivanry: 8 Aug, 2007 - 09:19 AM