(info) This AI is still in early stages of development.

object.super()

Call a superclass method or constructor from within the class method (of same name) or constructor.

 

Syntax

var result = this.super(args);

Parameters

ParameterTypeMandatoryDescriptionAPI Version
argsVariant(error)

Optionally specify any arguments to pass to the superclass constructor or method.

1.0

Return value

ValueTypeDescriptionAPI Version
<result>VariantThe return value of the superclass constructor/method.1.0
<error>ErrorAn error occurred.1.0

Example

// define superclass
var supervillain = function(name, color) {
	this.name = name;
	this.color = color || "Pink";
}
supervillain.prototype.toString = function() {
	return this.name;
}
supervillain.prototype.evilName = function() {
	return this.color+" "+this.name;
}
 
// define class
var villain = function(name, weapon, colour) {
	this.weapon = weapon || "Chuck Norris";
	this.super(name, colour);
}
villain.inherit(supervillain);
villain.prototype.evilName = function() {
	return "Evil "+this.super();
}
 
// make some instances
var bob = new villain("Bob", "Spoon", "Purple");
var alice = new villain("Alice");
 
bob.name; // "Bob"
bob.weapon; // "Spoon"
bob.color; // "Purple"
bob.toString(); // "Bob"
bob.evilName(); // "Evil Purple Bob"
 
alice.name; // "Alice"
alice.weapon; // "Chuck Norris"
alice.color; // "Pink"
alice.toString(); // "Alice"
alice.evilName(); // "Evil Pink Alice"

Notes

By default, super() will search 4 levels of the prototype chain to locate superclass methods. When it finds a super-method, it will cache the result so subsequent calls are significantly faster. Constructor super-constructors are automatically cached during inheritance for maximum instantiation performance.

If you want to increase the depth to which super() will search the prototype chain, you can set this.super to a number like so:

Changing maximum prototype search depth...
this.super = 10; // this.super() will search 10 levels deep
this.super = void super; // revert to default (4 levels)

The super() method can only be invoked from within a function (otherwise it will throw an error). It will only work on instances of classes that use a classical prototypal inheritance model.

uSuper() is designed for use on classes that use a classical prototypal inheritance model – specifically, the sort that function.inherit() sets up.

It must only be called from within a function.

Availability BETA

Requires:

Contents

Jump to:

Inheritance Model

Topics:

  • object.super()Call a superclass method or constructor from within the class method (of same name) or constructor.
  • function.inherit()Simplifies the implementation and use of classical Javascript prototypal inheritance.