Class Class
Create a new class instance
Constructor
Class
(
Class
-
Functions
Parameters:
-
Functions
Objectto add to the class
Returns:
Class:
Class instance
Example:
// Creation
var Person = new Class(
{
init: function(name)
{
this.name = name;
},
helloWorld: function()
{
return 'Hello';
},
helloWorld2: function()
{
return 'Hello2';
}
});
// Instantiation
var me = new Person('Daniel');
console.log('me.name = ', me.name); // Daniel
console.log('me.helloWorld() = ', me.helloWorld()); // Hello
console.log('me.helloWorld2() = ', me.helloWorld2()); // Hello2
console.log('me instanceof Person = ', me instanceof Person); // true
Methods
extend
(
Class
-
proto
Extend the current class. Expected to be called in the context of an existing class.
Parameters:
-
proto
ObjectPrototype methods of new class
Returns:
Class:
Class instance
Example:
// Base class
var Person = new Class(
{
init: function(name)
{
this.name = name;
},
helloWorld: function()
{
return 'Hello';
},
helloWorld2: function()
{
return 'Hello2';
}
});
// Class that inherits from base
var Ninja = Person.extend(
{
helloWorld2: function()
{
return 'Ninja helloWorld2, original = ' + this.parent();
},
helloWorld3: function()
{
return 'Ninja helloWorld3';
}
});
var awesome2 = new Ninja('Awesome');
console.log('awesome2.name = ', awesome2.name); // Awesome
console.log('awesome2.helloWorld() = ', awesome2.helloWorld()); // Hello
console.log('awesome2.helloWorld2() = ', awesome2.helloWorld2()); // Ninja helloWorld2, original = Hello2
console.log('awesome2.helloWorld3() = ', awesome2.helloWorld3()); // Ninja helloWorld3
console.log('awesome2 instanceof Ninja = ', awesome2 instanceof Ninja); // true
console.log('awesome2 instanceof Person = ', awesome2 instanceof Person); // true
mixin
(
private
-
source
-
destination
Copy functions from the source to the destination prototype. If functions already exist on the destination, a wrapper will be created that saves the old method into the .parent() method at runtime.
Parameters:
-
source
ObjectObject containing functions to add
-
destination
ObjectObject whose prototype will receive the new functions
wrapFn
(
Function
private
-
superFn
-
fn
Create a wrapper for this function. This wrapper saves the parent function into .parent (so it can be called from the overriding function) before calling the overriden function.
Parameters:
Returns:
Function:
Function that saves superFn to .parent, runs fn, then unsets parent