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.

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

else<br />

return "Engine is already running";<br />

},<br />

openThrottle: function(){<br />

if (this._currentSpeed0)<br />

this._currentSpeed--;<br />

}<br />

The last step is to register the class in the namespace, declaring the class that it<br />

inherits from is Vehicles.Boat:<br />

Vehicles.SpeedBoat.registerClass('Vehicles.SpeedBoat', Vehicles.Boat);<br />

Now you can declare SpeedBoat objects in your code, accessing their methods or the<br />

base methods of the Boat class. For example, the getBoatDetails method is available on<br />

the base class, so the SpeedBoat can access it like this:<br />

var MySpeedBoat = new Vehicles.SpeedBoat('Intimidator', '10', 'Arnie', '100cc');<br />

alert(MySpeedBoat.getBoatDetails());<br />

Inheritance like this has limitations—the only methods that your class can use are<br />

those in the class that it inherits from (<strong>and</strong> the class that its parent class inherits from,<br />

<strong>and</strong> so on). In cases where you might want to implement common methods across different<br />

classes, you cannot do it with inheritance. For example, the speedboat implements<br />

throttle opening <strong>and</strong> closing, <strong>and</strong> you may want to implement these for a motorcycle<br />

also. You can’t derive a motorcycle from a boat, so you’d end up having to implement<br />

them twice, <strong>and</strong> can end up with differences as a result, making for an untidy API (i.e.,<br />

Motorcycle could have ThrottleOpen while SpeedBoat has openThrottle), which isn’t desirable.<br />

The concept of interfaces is defined to help avoid this. You’ll see how this works in<br />

JavaScript in the next section.<br />

Using Interfaces in JavaScript<br />

An interface can be used to specify a function prototype that crosses different class types,<br />

<strong>and</strong> is not dependent on an inheritance tree.<br />

You can define functions that need to be in common across such classes—those that<br />

inherit from different base classes—by building them as an interface <strong>and</strong> having the class<br />

implement that interface.

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

Saved successfully!

Ooh no, something went wrong!