Skip to content

oatkiller/uberjs

Repository files navigation

What?

Use uberjs to imitate classes in JavaScript.

Methods

.subclass

.subclass(classConfig)

classConfig – {Object} – An object with any properties the subclass should have

If .constructor is provided, it will be used, if not, a default constructor will be provided

.subclasses

.subclasses(constructor)

constructor – {Function} – A constructor for a class that you would like the context Class to inherit from

Function.klass

Function.klass(classConfig)

*classConfig – Same as with .subclass

Creates a new class from a config

.uber

.uber(arguments,[arg1,arg2]);

arguments – {Arguments} – Takes the magic arguments object
[] – An optional array of arguments to call the uber method with.

If the second argument is not used, the same arguments as the sub method was called with will be passed to the super method

.getUber

.getUber(arguments)

Get a reference to the uber method

Get started

Include uber.js

Create a class in plain JavaScript


// Create a ‘class’ using plain JavaScript
var Widget = function (id) {
this.id = id;
};
Widget.prototype = {
set : function (value) {
this.value = value;
},
get : function () {
return this.value;
},
toString : function () {
return ’Widget: ’ + this.id;
}
};

// Or create a ‘class’ using the .klass shortcut
var Widget = Function.klass({
constructor : function (id) {
this.id = id;
},
set : function (value) {
this.value = value;
},
get : function () {
return this.value;
},
toString : function () {
return ’Widget: ’ + this.id;
}
});

// make a subclass with .subclass
var ValidatingWidget = Widget.subclass({
validator : function () {
throw new Error(‘must be overridden’);
},
set : function (value) {
if (this.validator(value)) {
// call the overridden method with this.uber(arguments);
this.uber(arguments);
} else {
throw new Error(‘validation failed’);
}
}
});

// .subclass works on subclasses
var NumberWidget = ValidatingWidget.subclass({
validator : function (value) {
// must be a number and not Not a Number
return typeof value === ‘number’ && !isNaN(value);
}
});

// You can set inheritance on an existing class using .subclasses
// how you create a class is not important
var StringWidget = function () {};
StringWidget.prototype.validator = function (value) {
return typeof value === ‘string’;
};
// .subclasses takes a super class
StringWidget.subclasses(ValidatingWidget);

// this.uber can take a second optional array of arguments
var Person = Function.klass({
setInfo : function (name,age) {
this.name = name;
this.age = age;
}
});

// call the overridden method with name, age, instead of the arguments that this was called with
var Employee = Person.subclass({
setInfo : function (title,name,age) {
this.title = title;
this.uber(arguments,[name,age]);
}
});

// getUber can be used to get the overridden method
var Employee = Person.subclass({
setInfo : function (title,name,age) {
this.title = title;
this.getUber(arguments).call(this,name,age);
}
});


About

A simple JavaScript classical-inheritance library.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published