new-base-class is deprecated.
base-class-extend!
usebase-class-extend - npm
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 ...
INSTALL:
$ npm install base-class-extend
or
http://lightspeedworks.github.io/base-class-extend/lib/base-class-extend.js
USAGE:
var BaseClass = ;
method: Class.extend(name, proto, classProps)
Define new class (constructor function) that inherited from Base Class.
Format
var YourClass = BaseClass;var YourSubClass = YourClass;
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, ctor or constructor: constructor function, optional
- get prop(): getter function, optional
- set prop(value): setter function, optional
- any methods: any method or member function, optional
- classProps: 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 classProps also, if you omit proto.
You have to specify proto or {}
, if you want to specify classProps.
Returns
The newly defined class (constructor function). (Your class is subclass of BaseClass)
Details
A simple and quick sample:
var BaseClass = ; var MyClass = BaseClass; var myObj = 5;myObjvalue++; // 5 -> 6myObj;myObjvalue++; // 6 -> 7 throws Error
method: Class.new(...) or Class.create(...)
Create an object, instance of the Class.
Format
var YourClass = BaseClass;var yourObj = YourClass; // orvar yourObj = YourClass; // orvar yourObj = ; // orvar yourObj = ;// required: default constructor or right defined constructor
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
Objectextend = BaseClassextend;var SimpleClass = Object; // or simplyvar SimpleClass = BaseClassextend;
inherits from Array class
Arrayextend = BaseClassextend;var CustomArray = Array; // or simplyvar CustomArray = BaseClassextend; var ca = 1 2 3;// returns [1, 2, 3] like custom array.
inherits from Error class
Errorextend = BaseClassextend;var CustomError = Error; // or simplyvar CustomError = BaseClassextend; var ce = 'message';
inherits from EventEmitter class
var EventEmitter = EventEmitter;EventEmitterextend = BaseClassextend;var CustomEventEmitter = EventEmitter; // or simplyvar CustomEventEmitter = BaseClassextend;
inherits from all other class or constructor ... Function
Functionprototypeextend = BaseClassextend; var SimpleClass = Object;var CustomArray = Array;var CustomError = Error; var EventEmitter = EventEmitter;var CustomEventEmitter = EventEmitter;
method: this.private(proto)
You can define private variables, hidden variables.
Also support getter/setter, and normal methods to access private variables.
Format
// defined in 'new' method or 'constructor' functionvar private1;this;
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;
property: this.constructor
Get constructor function. (Class)
Format
var MyClass = BaseClass;var o1 = ;console; // -> true
Returns
The constructor function. (Class)
property: this.constructors
Get an array of constructor functions. (Classes)
Format
var MyClass = BaseClass;var o1 = ;var classes = o1constructors;console; // -> trueconsole; // -> trueconsole; // -> true
Returns
An array of constructor functions. (Classes)
property: Class.constructors
Get an array of constructor functions.
Format
var MyClass = BaseClass;var classes = MyClassconstructors;console; // -> trueconsole; // -> trueconsole; // -> true
Returns
An array of constructor functions.
EXAMPLES:
// Animal // BaseClassvar BaseClass = ; // SimpleClassvar SimpleClass = BaseClass;var s1 = ; // Animalvar Animal = BaseClass; // -> Animal class initvar a1 = 'Annie';a1; // -> My name is AnnieAnimal; // -> Animal class method // Bearvar Bear = Animal;var b1 = ; // new lessb1; // -> My name is Pooh var Cat = Animal;var c1 = Cat;c1; // -> My name is Kitty var Dog = Animal; // -> Dog initvar d1 = Dog; // Class method new calld1; // -> My name is HachiDog; // -> Animal class method, Dog class methodDog; // -> Animal class method
// Vector2D/Vector3D // BaseClassvar BaseClass = ; // sample: JavaScript Object.defineProperty - SONICMOOV LAB// http://lab.sonicmoov.com/development/javascript-object-defineproperty/ var Vector2D = BaseClass; var v2 = 3 4;console;v2;console;v2;console; var Vector3D = Vector2D; var v3 = 3 4 5;console;
LICENSE:
MIT