BaseClass.extend defines class in JavaScript.
This is simple module providing a simple Class function to
simplify class definition in JavaScript.
Supports getter/setter.
Easy to use, easy to inherit/extend.
Also supports inheritance from Array
, Error
, or Node.js events.EventEmitter
.
no difficult keywords,
no constructor
, no prototype
, no __proto__
,
no Object.defineProperty
, no Object.setPrototypeOf
, etc ...
Supports Chrome, Firefox, ie11/9/8/6 and Node.js/io.js.
Japanese version/■日本語版はこちら■
INSTALL:
$ npm install base-class-extend
or
http://lightspeedworks.github.io/base-class-extend/lib/base-class-extend.js
<script src="http://lightspeedworks.github.io/base-class-extend/lib/base-class-extend.js"></script>
USAGE:
var BaseClass = require('base-class-extend');
method: Class.extend(name, proto, staticProps)
Define new class (constructor function) that inherited from Base Class.
Format
var YourClass = BaseClass.extend([name], [proto], [staticProps]);
var YourSubClass = YourClass.extend([name], [proto], [staticProps]);
Parameters
- BaseClass: Base class or Super class for inherits
- name: string name of your class, optional
- proto: the prototype object for your class, optional
- new or constructor: constructor function, optional
- get prop(): getter function, optional
- set prop(value): setter function, optional
- any methods: any method or member function, optional
- staticProps: the object for class or static properties, optional
- init: initialize function, optional
- get prop(): getter function, optional
- set prop(value): setter function, optional
- any methods: any static method or class function, optional
You have to omit staticProps also, if you omit proto.
You have to specify proto or {}
, if you want to specify staticProps.
Returns
The newly defined class (constructor function). (Your class is subclass of BaseClass)
Details
A simple and quick sample:
var BaseClass = require('base-class-extend');
var MyClass = BaseClass.extend({
new: function MyClass(value) {
this.value = value;
},
show: function show() {
console.log(this.value);
},
get value() { return this._value; },
set value(value) {
if (value < 1 || value > 6)
throw new RangeError('Out of range');
this._value = value; },
});
var myObj = new MyClass(5);
myObj.value++;
myObj.show();
myObj.value++;
method: Class.new(...) or Class.create(...)
Create an object, instance of the Class.
Format
var YourClass = BaseClass.extend('YourClass');
var yourObj = YourClass.new();
var yourObj = YourClass.create();
var yourObj = new YourClass();
var yourObj = YourClass();
Parameters
- arguments...: pass to constructor function, optional
Returns
Your new object, instance of the Class.
without BaseClass, inherits from Object, or other Classes
inherits from Object class
var BaseClass = require('base-class-extend').extendPrototype(Object);
var SimpleClass = Object.extend('SimpleClass');
var SimpleClass = BaseClass.extend.call(Object, 'SimpleClass');
inherits from Array class
var BaseClass = require('base-class-extend').extendPrototype(Array);
var CustomArray = Array.extend('CustomArray');
var CustomArray = BaseClass.extend.call(Array, 'CustomArray');
var ca = new CustomArray(1, 2, 3);
inherits from Error class
var BaseClass = require('base-class-extend').extendPrototype(Error);
var CustomError = Error.extend('CustomError');
var CustomError = BaseClass.extend.call(Error, 'CustomError');
var ce = new CustomError('message');
inherits from EventEmitter class
var EventEmitter = require('events').EventEmitter;
var BaseClass = require('base-class-extend').extendPrototype(EventEmitter);
var CustomEventEmitter = EventEmitter.extend('CustomEventEmitter');
var CustomEventEmitter = BaseClass.extend.call(EventEmitter, 'CustomEventEmitter');
inherits from all other class or constructor ... Function
var BaseClass = require('base-class-extend').extendPrototype();
var SimpleClass = Object.extend('SimpleClass');
var CustomArray = Array.extend('CustomArray');
var CustomError = Error.extend('CustomError');
var EventEmitter = require('events').EventEmitter;
var CustomEventEmitter = EventEmitter.extend('CustomEventEmitter');
method: this.addPrototype(proto)
You can define private variables, hidden variables.
Also support getter/setter, and normal methods to access private variables.
Format
{
constructor: function () {
var private1;
this.addPrototype({
method1: function method1() {
console.log(private1); },
get prop1() { return private1; },
set prop1(val) { private1 = val; },
});
}
}
Parameters
- proto: the prototype object contains methods accessing private variables, required
- get prop(): getter function, optional
- set prop(value): setter function, optional
- any methods: any method or member function, optional
Returns
The prototype object you passed.
Details
Sample:
var YourClass = BaseClass.extend({
new: function YourClass() {
var private1 = 123;
var private2 = 'abc';
this.addPrototype({
get private1() { return private1; },
set private1(val) { private1 = val; },
get private2() { return private2; },
});
},
});
method: this.private(proto)
You can define private variables, hidden variables.
Also support getter/setter, and normal methods to access private variables.
Format
{
constructor: function () {
var private1;
this.private({
method1: function method1() {
console.log(private1); },
get prop1() { return private1; },
set prop1(val) { private1 = val; },
});
}
}
Parameters
- proto: the prototype object contains methods accessing private variables, required
- get prop(): getter function, optional
- set prop(value): setter function, optional
- any methods: any method or member function, optional
Returns
The prototype object you passed.
Details
Sample:
var YourClass = BaseClass.extend({
new: function YourClass() {
var private1 = 123;
var private2 = 'abc';
this.private({
get private1() { return private1; },
set private1(val) { private1 = val; },
get private2() { return private2; },
});
},
});
EXAMPLES:
var BaseClass = require('base-class-extend');
var SimpleClass = BaseClass.extend('SimpleClass');
var s1 = new SimpleClass();
var Animal = BaseClass.extend({
new: function Animal(name) {
if (!(this instanceof Animal))
return Animal.new.apply(Animal, arguments);
BaseClass.apply(this);
this.name = name;
},
get name() { return this._name; },
set name(name) { this._name = name; },
introduce: function () {
console.log('My name is ' + this.name);
},
}, {
init: function () {
console.log('Animal class init');
},
animalClassMethod: function () {
console.log('Animal class method');
}
});
var a1 = new Animal('Annie');
a1.introduce();
Animal.animalClassMethod();
var Bear = Animal.extend('Bear');
var b1 = Bear('Pooh');
b1.introduce();
var Cat = Animal.extend({
new: function Cat() {
if (!(this instanceof Cat))
return Cat.new.apply(Cat, arguments);
Cat.super_.apply(this, arguments);
}
});
var c1 = Cat.new('Kitty');
c1.introduce();
var Dog = Animal.extend({
new: function Dog() {
if (!(this instanceof Dog))
return Dog.new.apply(Dog, arguments);
Dog.super_.apply(this, arguments);
},
}, {
init: function () {
console.log('Dog class init');
},
dogClassMethod: function () {
this.animalClassMethod();
console.log('Dog class method');
}
});
var d1 = Dog.new('Hachi');
d1.introduce();
Dog.dogClassMethod();
Dog.animalClassMethod();
var BaseClass = require('base-class-extend');
var Vector2D = BaseClass.extend({
new: function Vector2D(x, y) {
this._length = 0;
this._changed = true;
this._x = x;
this._y = y;
},
get x() { return this._x; },
set x(x) { this._x = x; this._changed = true; },
get y() { return this._y; },
set y(y) { this._y = y; this._changed = true; },
get length() {
if (this._changed) {
this._length = Math.sqrt(this._x * this._x + this._y * this._y);
this._changed = false;
}
return this._length;
},
set: function (x, y) { this._x = x; this._y = y; this._changed = true; },
});
var v2 = new Vector2D(3, 4);
console.log('V2D(3, 4):', v2.length);
v2.set(1, 2);
console.log('V2D(1, 2):', v2.length);
v2.set(1, 1);
console.log('V2D(1, 1):', v2.length);
var Vector3D = Vector2D.extend({
new: function Vector3D(x, y, z) {
Vector2D.call(this, x, y);
this._z = z;
},
get length() {
if (this._changed) {
this._length = Math.sqrt(this._x * this._x + this._y * this._y + this._z * this._z);
this._changed = false;
}
return this._length;
},
set: function (x, y, z) { this._x = x; this._y = y; this._z = z; this._changed = true; },
});
var v3 = new Vector3D(3, 4, 5);
console.log('V3D(3, 4, 5):', v3.length);
SEE ALSO:
LICENSE:
MIT