08.01.2015 Views

Beginning Web Development, Silverlight, and ASP.NET AJAX

Beginning Web Development, Silverlight, and ASP.NET AJAX

Beginning Web Development, Silverlight, and ASP.NET AJAX

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

332<br />

CHAPTER 14 ■ JAVASCRIPT PROGRAMMING WITH <strong>ASP</strong>.<strong>NET</strong> <strong>AJAX</strong><br />

car, such as its color, a method is something that you can perform on the car, such as<br />

driving it, <strong>and</strong> an event is something the car informs you of <strong>and</strong> that you can respond to,<br />

such as the little red light that comes on when you are nearly out of gas.<br />

In <strong>ASP</strong>.<strong>NET</strong> <strong>AJAX</strong>, properties are either set using dot notation or accessors. The rule<br />

of thumb is that you can use dot syntax when the property is a primitive type (such as a<br />

string), as follows:<br />

MyCar.Color="Black";<br />

Accordingly, you use the accessor syntax when the property is a complex object. The<br />

accessor syntax uses get_ <strong>and</strong> set_ methods to get <strong>and</strong> set the value of the property. For<br />

example, if your color settings are an object, then you will use the following:<br />

MyCar.set_Color(ColorObject);<br />

Methods are functions that you can call on an object, <strong>and</strong> that don’t necessarily need<br />

a return parameter. For example, if you want to start the ignition on your car, you would<br />

do it using a method like this:<br />

MyCar.startEngine();<br />

Events are actions that take place on your object that raise a notification. In Java-<br />

Script, you specify the h<strong>and</strong>ler for the event by appending add to the event, specifying the<br />

h<strong>and</strong>ler name. For example, if you want to h<strong>and</strong>le an event for the fuel running low in<br />

your car, you would write code like this:<br />

MyCar.fuelLow.add(FuelLowH<strong>and</strong>ler);<br />

And you would then implement the specified function like this:<br />

Function FuelLowH<strong>and</strong>er(sender,args)<br />

{<br />

...<br />

}<br />

Using Namespaces in JavaScript<br />

Namespaces are a methodology that you can use to group similar classes together to<br />

make for easier management. For example, if you are building a library of classes for<br />

common objects, such as car, boat, house, person, fruit, <strong>and</strong> dog, then it is a good idea to<br />

group these into different namespaces, for manageability <strong>and</strong> extensibility. For example,<br />

you could have a Vehicles namespace that car <strong>and</strong> boat live in. Then, if you were to create<br />

an airplane class, it would live in the same namespace. Also, consider the scenario where

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!