HTML and CSS Reference
In-Depth Information
But this doesn't mean that JavaScript has all the trappings of object-oriented programming (OOP) that you
might expect. First, it doesn't have a classical inheritance model. Second, it doesn't have a standard constructor
mechanism, relying instead on either constructor functions or object literals.
Instead of classical inheritance, JavaScript implements prototypical inheritance , meaning you can create
an object that represents the prototype, or blueprint, for a set of descendant objects that all share the same base
functionality
Classical Versus Prototypical Inheritance
Most popular object-oriented languages used today, including Java and C++, rely on classical inheritance, which
means object behavior is defined by creating explicit classes and instantiating objects from those classes.
JavaScript has a much more fluid method of defining classes based on the idea of prototypes, which means you
create an actual object that behaves the way you want and then create child objects off of that.
Because methods are just regular JavaScript objects, many times developers also simply copy attributes
from other objects to fake Java-style interfaces or multiple inheritance. This flexibility shouldn't necessarily be
viewed as a problem; rather, it means that developers have a lot of flexibility for how to create objects and can
pick the best method for the specific use case.
Alien Invasion uses constructor functions combined with prototypical inheritance where it makes sense.
Using object prototypes can make object creation up to 50 times faster and provides memory savings, but it
is also more restricting because you can't use closures to access and protect data. Closures are a feature of
JavaScript that allows you to keep variables in a method around for later use even when a method has finished
execution.
Chapter 9, “Bootstrapping the Quintus Engine: Part I,” discusses object-creation patterns in more detail, but
for now just realize that the use of different methods is intentional.
Taking Advantage of Duck Typing
There's a famous saying that if it walks like a duck and talks like a duck, then it must be a duck. When program-
ming in strongly-typed languages, there's no doubt whether it's a duck—it must be an instance of the “Duck”
class, or if you program in Java, implement the iDuck interface.
In JavaScript, a dynamically-typed language, parameters, and references are not type-checked, meaning you
can pass any type of object as a parameter to any function, and that function happily treats that object like the
type of whatever object it was expecting until something blows up.
This flexibility can be both a good and a bad thing. It's a bad thing when you end up with cryptic error mes-
sages or errors during run time. It's a good thing when you use that flexibility to keep a shallow inheritance tree
but can still share code. The idea of using objects based on their external interface rather than their type is called
duck typing .
Alien Invasion uses this idea in a couple of places: game screens and sprites. The game treats anything that
responds to method calls of step() and draw() as valid game screen objects or valid sprites. Using duck
typing for game screens enables Alien Invasion to treat title screens and in-game screens as the same type of
object, making it easy to switch between levels and title screens. Similarly, using duck typing for sprites means
that the game can be flexible with what can be added to a game board, including the player, enemies, projectiles,
and HUD elements. HUD, which is short for Heads Up Display, is the term commonly used for elements that
sit on top of the game, such as number of lives left and the player's score.
Search WWH ::




Custom Search