3.5. Using Prototypes
The function type in JavaScript allows you to define custom types. JavaScript does not support static type checking, so you can be creative in creating and using custom types on the fly. The object-oriented programming paradigm has made programming at a higher level the standard for many developers and is more appropriate than procedural programming for most tasks. JavaScript does not support inheritance as other object-oriented languages do, but functions can be used to model data types. As a dynamic language, JavaScript opens the door for some interesting type definitions that can make the code look more familiar to C# and Visual Basic developers.
Listing 3-16 includes a simple definition of an album object in C#. There are string variables for storing the title and artist for the album. For this class, there is also a constructor that takes the artist and title and stores them in the local variables.
Example 3-16. A C# example of an Album object
using System; public class Album { private string _title = String.Empty; private string _artist = String.Empty; public Album(string title, string artist) { this._title = title; this._artist = artist; } } |
You can model the same object in JavaScript using a function. Listing 3-17 has a JavaScript function with two arguments: title and artist. This is just like the constructor in C# presented in Listing 3-16. The JavaScript function takes the two arguments and assigns them to the local variables declared inside the ...
Get Professional ASP.NET 3.5 AJAX now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.