Exforsys.com
 
Home Tutorials Microsoft AJAX
 

Microsoft AJAX Library - Object-Oriented JavaScript

 

Object-Oriented JavaScript

Objects and classes are implemented differently in JavaScript than in languages such as C#, VB.NET, Java, or C++. However, when it comes to using them, you'll feel on familiar ground. You create objects using the new operator, and you call their methods, or access their fields using the syntax you already know from C#. Here are a few examples of creating objects in JavaScript:



// create a generic object
var obj = new Object();
// create a Date object
var oToday = new Date();
// create an Array object with 3 elements
var oMyList = new Array(3);
// create an empty String object
var oMyString = new String();


Object creation is, however, the only significant similarity between JavaScript objects and those of "typical" OOP languages. The upcoming JavaScript 2.0 will reduce the differences by introducing the concept of classes, private members, and so on, but until then we have to learn how to live without them.


Objects in JavaScript have the following particularities. In the following pages we'll discuss each of them in detail:


  • JavaScript code is not compiled, but parsed. This allows for flexibility when it comes to creating or altering objects. As you'll see, it's possible to add new members or functions to an object or even several objects by altering their prototype, on the fly.
    .
  • JavaScript doesn't support the notion of classes as typical OOP languages do. In JavaScript, you create functions that can behave—in many cases—just like classes. For example, you can call a function supplying the necessary parameters, or you can create an instance of that function supplying those parameters. The former case can be associated with a C# method call, and the later can be associated with instantiating a class supplying values to its constructor.
    .
  • JavaScript functions are first-class objects. In English, this means that the function is regarded, and can be manipulated, just as like other data types. For example, you can pass functions as parameters to other functions, or even return functions. This concept may be difficult to grasp since it's very different from the way C# developers normally think of functions or methods, but you'll see that this kind of flexibility is actually cool.
    .
  • JavaScript supports closures.
    .
  • JavaScript supports prototypes.


Ray Djajadinata's JavaScript article at http://msdn.microsoft.com/msdnmag/ issues/07/05/JavaScript/ covers the OOP features in JavaScript very well, and you can refer to it if you need another approach at learning these concepts.



Read Next: Microsoft AJAX Library - JavaScript Functions



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape