Capitalizing on JavaScript's prototypal inheritance
Posted
by
keithjgrant
on Game Development
See other posts from Game Development
or by keithjgrant
Published on 2011-03-16T18:25:26Z
Indexed on
2011/03/17
0:20 UTC
Read the original article
Hit count: 336
JavaScript has a class-free object system in which objects inherit properties directly from other objects. This is really powerful, but it is unfamiliar to classically trained programmers. If you attempt to apply classical design patterns directly to JavaScript, you will be frustrated. But if you learn to work with JavaScript's prototypal nature, your efforts will be rewarded.
...
It is Lisp in C's clothing.
What does this mean for a game developer working with canvas and HTML5? I've been looking over this question on useful design patterns in gaming, but prototypal inheritance is very different than classical inheritance, and there are surely differences in the best way to apply some of these common patterns.
For example, classical inheritance allows us to create a moveableEntity
class, and extend that with any classes that move in our game world (player
, monster
, bullet
, etc.). Sure, you can strongarm JavaScript to work that way, but in doing so, you are kind of fighting against its nature. Is there a better approach to this sort of problem when we have prototypal inheritance at our fingertips?
© Game Development or respective owner