npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

base-class-extend

v0.0.21

Published

Base Class constructor for easy class definition - supports getter/setter, inherit/extend Array Error or EventEmitter etc

Downloads

21

Readme

base-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 prototype, no __proto__, no Object.defineProperty, no Object.setPrototypeOf, etc ... needs constructor only.

Supports Google Chrome, Mozilla Firefox, Microsoft ie11/10/9/8/7/6 and Node.js/io.js.

Japanese version/■日本語版はこちら■

base-class.png

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
    • constructor or new: 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({
  constructor: function MyClass(value) {
    this.value = value; // via setter
  },
  show: function show() {
    console.log(this.value); // via getter
  },
  // getter
  get value() { return this._value; },
  // setter
  set value(value) {
    if (value < 1 || value > 6)
      throw new RangeError('Out of range');
    this._value = value; },
});

var myObj = new MyClass(5);
myObj.value++; // 5 -> 6
myObj.show();
myObj.value++; // 6 -> 7 throws Error

method: Class.create(...) or Class.new(...)

Create an object, instance of the Class.

Format

var YourClass = BaseClass.extend('YourClass');
var yourObj = YourClass.create();

// or
var yourObj = YourClass.new();

// or
var yourObj = new YourClass();

// or
var yourObj = YourClass();
// 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

var BaseClass = require('base-class-extend').extendPrototype(Object);
//  or
// Object.extend = BaseClass.extend;

var SimpleClass = Object.extend('SimpleClass');

// or simply
var SimpleClass = BaseClass.extend.call(Object, 'SimpleClass');

inherits from Array class

var BaseClass = require('base-class-extend').extendPrototype(Array);
//  or
// Array.extend = BaseClass.extend;

var CustomArray = Array.extend('CustomArray');

// or simply
var CustomArray = BaseClass.extend.call(Array, 'CustomArray');

var ca = new CustomArray(1, 2, 3);
// returns [1, 2, 3] like custom array.

inherits from Error class

var BaseClass = require('base-class-extend').extendPrototype(Error);
//  or
// Error.extend = BaseClass.extend;

var CustomError = Error.extend('CustomError');

// or simply
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);
//  or
// EventEmitter.extend = BaseClass.extend;

var CustomEventEmitter = EventEmitter.extend('CustomEventEmitter');

// or simply
var CustomEventEmitter = BaseClass.extend.call(EventEmitter, 'CustomEventEmitter');

inherits from all other class or constructor ... Function

var BaseClass = require('base-class-extend').extendPrototype();
//  or
// Function.prototype.extend = BaseClass.extend;

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

// defined in 'constructor' method or 'new' function
{
  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({
  constructor: function YourClass() {
    var private1 = 123; // access via getter/setter
    var private2 = 'abc'; // access via getter, no setter
    this.addPrototype({
      get private1() { return private1; }, // getter
      set private1(val) { private1 = val; }, // setter
      get private2() { return private2; }, // getter
    });
  },
});

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 'constructor' method or 'new' function
{
  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({
  constructor: function YourClass() {
    var private1 = 123; // access via getter/setter
    var private2 = 'abc'; // access via getter, no setter
    this.private({
      get private1() { return private1; }, // getter
      set private1(val) { private1 = val; }, // setter
      get private2() { return private2; }, // getter
    });
  },
});

EXAMPLES:

// Animal

// BaseClass
var BaseClass = require('base-class-extend');

// SimpleClass
var SimpleClass = BaseClass.extend('SimpleClass');
var s1 = new SimpleClass();

// Animal
var Animal = BaseClass.extend({
  constructor: function Animal(name) {
    if (!(this instanceof Animal))
      return Animal.create.apply(Animal, arguments);
    BaseClass.apply(this); // or Animal.super_.apply(this);
    this.name = name;
  },
  get name() { return this._name; }, // getter
  set name(name) { this._name = name; }, // setter
  introduce: function () {
    console.log('My name is ' + this.name);
  },
}, {
  init: function () {
    console.log('Animal class init');
  },
  animalClassMethod: function () {
    console.log('Animal class method');
  }
}); // -> Animal class init
var a1 = new Animal('Annie');
a1.introduce(); // -> My name is Annie
Animal.animalClassMethod(); // -> Animal class method

// Bear
var Bear = Animal.extend('Bear');
var b1 = Bear('Pooh'); // new less
b1.introduce(); // -> My name is Pooh

var Cat = Animal.extend({
  constructor: function Cat() {
    if (!(this instanceof Cat))
      return Cat.create.apply(Cat, arguments);
    Cat.super_.apply(this, arguments);
  }
});
var c1 = Cat.create('Kitty');
c1.introduce(); // -> My name is Kitty

var Dog = Animal.extend({
  constructor: function Dog() {
    if (!(this instanceof Dog))
      return Dog.create.apply(Dog, arguments);
    Dog.super_.apply(this, arguments);
  },
}, {
  init: function () {
    console.log('Dog class init');
  },
  dogClassMethod: function () {
    this.animalClassMethod();
    console.log('Dog class method');
  }
}); // -> Dog init
var d1 = Dog.create('Hachi'); // Class method create call
d1.introduce(); // -> My name is Hachi
Dog.dogClassMethod(); // -> Animal class method, Dog class method
Dog.animalClassMethod(); // -> Animal class method
// Vector2D/Vector3D

// BaseClass
var BaseClass = require('base-class-extend');

// sample: JavaScript Object.defineProperty - SONICMOOV LAB
// http://lab.sonicmoov.com/development/javascript-object-defineproperty/

var Vector2D = BaseClass.extend({
  constructor: 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({
  constructor: 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:

get-constructors - npm

LICENSE:

MIT