Welcome to Dream.In.Code
Getting Help is Easy!

Join 118,310 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,704 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Intro to JavaScript for ASP AJAX

 
Reply to this topicStart new topic

> Intro to JavaScript for ASP AJAX, Tutorial on Gotcha's for beginning ASP AJAX Developers

Shivanry
Group Icon



post 8 Aug, 2007 - 09:17 AM
Post #1


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


Attached File(s)
Attached File  IntroJavaScriptandOOP.doc ( 35k ) Number of downloads: 174
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

William_Wilson
Group Icon



post 8 Aug, 2007 - 12:36 PM
Post #2
this should be submitted into the tutorial section, then the data can be verified and you will be given credit for it.
Go to the top of the page
+Quote Post

PennyBoki
Group Icon



post 26 Aug, 2007 - 03:47 PM
Post #3
Hi, I think you should increase the font size, it is not readable at this moment.
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 10/10/08 11:54AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month