Q.Class Class
The base Class object
Quintus uses the Simple JavaScript inheritance Class object, created by John Resig and described on his blog:
http://ejohn.org/blog/simple-javascript-inheritance/
The class is used wholesale, with the only differences being that instead
of appearing in a top-level namespace, the Class
object is available as
Q.Class
and a second argument on the extend
method allows for adding
class level methods and the class name is passed in a parameter for introspection
purposes.
Classes can be created by calling Q.Class.extend(name,{ .. })
, although most of the time
you'll want to use one of the derivitive classes, Q.Evented
or Q.GameObject
which
have a little bit of functionality built-in. Q.Evented
adds event binding and
triggering support and Q.GameObject
adds support for components and a destroy method.
The main things Q.Class get you are easy inheritance, a constructor method called init()
,
dynamic addition of a this._super method when a method is overloaded (be careful with
this as it adds some overhead to method calls.) Calls to instanceof
also all
work as you'd hope.
By convention, classes should be added onto to the Q
object and capitalized, so if
you wanted to create a new class for your game, you'd write:
Q.Class.extend("MyClass",{ ... });
Examples:
Q.Class.extend("Bird",{
init: function(name) { this.name = name; },
speak: function() { console.log(this.name); },
fly: function() { console.log("Flying"); }
});
Q.Bird.extend("Penguin",{
speak: function() { console.log(this.name + " the penguin"); },
fly: function() { console.log("Can't fly, sorry..."); }
});
var randomBird = new Q.Bird("Frank"),
pengy = new Q.Penguin("Pengy");
randomBird.fly(); // Logs "Flying"
pengy.fly(); // Logs "Can't fly,sorry..."
randomBird.speak(); // Logs "Frank"
pengy.speak(); // Logs "Pengy the penguin"
console.log(randomBird instanceof Q.Bird); // true
console.log(randomBird instanceof Q.Penguin); // false
console.log(pengy instanceof Q.Bird); // true
console.log(pengy instanceof Q.Penguin); // true
Simple JavaScript Inheritance By John Resig http://ejohn.org/ MIT Licensed.
Inspired by base2 and Prototype
Methods
extend
-
className
-
properties
-
[classMethods]
Create a new Class that inherits from this class
Parameters:
-
className
String -
properties
Object- hash of properties (init will be the constructor)
-
[classMethods]
Object optional- optional class methods to add to the class
isA
-
className
See if a object is a specific class
Parameters:
-
className
String- class to check against