Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

base-class-extend

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

base-class-extend - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

test/base-class-object-test.js

42

lib/base-class.js

@@ -45,5 +45,5 @@ // base-class.js

var super_ = this;
if (typeof super_ !== 'function')
super_ = Object;
var superCtor = this;
if (typeof superCtor !== 'function')
superCtor = Object;

@@ -54,9 +54,8 @@ if (!proto) proto = {};

proto.hasOwnProperty('constructor') && proto.constructor ||
Function('proto, super_, new_',
Function('proto, superCtor, new_',
'return function ' + name + '() {\n' +
' if (!(this instanceof proto.constructor)) \n' +
' return new_.apply(proto.constructor, arguments) \n' +
' return super_.apply(this, arguments) || this;\n' +
'}'
)(proto, super_, new_);
' return new_.apply(proto.constructor, arguments); \n' +
' return superCtor.apply(this, arguments), this; }')
(proto, superCtor, new_);
if (typeof ctor !== 'function')

@@ -68,6 +67,5 @@ throw new TypeError('constructor must be a function');

' if (!(this instanceof proto.constructor)) \n' +
' return new_.apply(proto.constructor, arguments) \n' +
' return ctor.apply(this, arguments) || this;\n' +
'}'
)(proto, ctor, new_);
' return new_.apply(proto.constructor, arguments); \n' +
' return ctor.apply(this, arguments), this; }')
(proto, ctor, new_);
}

@@ -81,3 +79,3 @@ delete proto.constructor;

// inherits
setProto(proto, super_.prototype);
setProto(proto, superCtor.prototype);

@@ -87,3 +85,3 @@ // inherits class methods

// constructor.__proto__ -> for inherits class methods
setProto(ctor, super_);
setProto(ctor, superCtor);
}

@@ -93,3 +91,3 @@ else {

setProto(ctor, classProps);
setProto(classProps, super_);
setProto(classProps, superCtor);

@@ -111,6 +109,6 @@ // class initializer

!classProps[key].name) {
classProps[key] = Function('fn, super_',
classProps[key] = Function('fn, superCtor',
'return function ' + key + '_() {\n' +
' return fn.apply(this, arguments); }')
(classProps[key], super_);
(classProps[key], superCtor);
}

@@ -120,4 +118,7 @@ }

if (!('extend' in ctor)) ctor.extend = extend;
if (!('new' in ctor)) ctor.new = new_;
// constructor.super_ -> for points super class
setConst(ctor, 'super_', super_);
setConst(ctor, 'super_', superCtor);

@@ -129,7 +130,6 @@ return ctor;

var obj = Object.create(this.prototype);
return this.apply(obj, arguments) || obj;
return this.apply(obj, arguments), obj;
}
var BaseClass = extend(
{new: function BaseClass() {}},
var BaseClass = extend('BaseClass', {},
{extend: extend, new: new_});

@@ -136,0 +136,0 @@

{
"name": "base-class-extend",
"version": "0.0.1",
"description": "new Base Class for easy class definition - inherits, extend",
"version": "0.0.2",
"description": "Base Class constructor for easy class definition - inherits, extend",
"main": "lib/base-class.js",

@@ -6,0 +6,0 @@ "scripts": {

base-class-extend
====
BaseClass is utility for simple class definition.
easy to use, easy to inherits/extend,
BaseClass define classes in JavaScript.
This is simple module providing a simple Class function to
simplify class definition in JavaScript.
Easy to use, easy to inherits/extend.
no difficult keywords,
no `constructor`, no `prototype`, no `__proto__`,
no `Object.defineProperty`, no `Object.setPrototypeOf`, etc ...
# INSTALL:
```bash
npm install base-class-extend
```
# USAGE:
```js
var BaseClass = require('base-class-extend');
var BaseClass = require('base-class-extend');
```
## BaseClass.extend([name], proto, [classProps])
# method: BaseClass.extend
### [name] - SubClass name: string
## Prototype
### proto - instance prototype properties: object
```js
var YourClass = BaseClass.extend([name], prototype, [classProps]);
```
#### name / constructor - constructor function: function
#### others - instance method functions: function
## Parameters
### [classProps] - class properties: object
+ BaseClass: Base class or Super class for inherits
#### init / initialize - class initialize function: function
#### others - class method functions: function
+ name: string name of your class, optional
## BaseClass.new(...)
+ prototype: the prototype object for your class, optional
# SAMPLE:
+ classProps: class or static properties object, optional
## Returns
The newly defined class (Your class is subclass of BaseClass)
## Details
A simple and quick sample:
```js
var BaseClass = require('base-class-extend');
// SimpleClass
var SimpleClass = BaseClass.extend();
var s1 = new SimpleClass();
// Animal
var Animal = BaseClass.extend({
new: function Animal(name) {
if (!(this instanceof Animal))
return Animal.new.apply(Animal, arguments);
this.name = name;
var MyClass = BaseClass.extend({
new: function MyClass(value) {
this.value = value; // via setter
},
get name() { return this._name; }, // getter
set name(name) { this._name = name; }, // setter
introduce: function () {
console.log('My name is ' + this.name);
show: function show() {
console.log(this.value); // via getter
},
}, {
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
get value() { return this._value; },
set value(value) {
if (!(value >= 1 && value <= 10))
throw new Error('');
this._value = value; },
});
// Bear
var Bear = Animal.extend('Bear');
var b1 = Bear('Pooh'); // new less
b1.introduce(); // -> My name is Pooh
var myObj = new MyClass(5);
myObj.value++;
myObj.show();
```
var Cat = Animal.extend({
new: function Cat() {
if (!(this instanceof Cat))
return Cat.new.apply(Cat, arguments);
return Cat.super_.apply(this, arguments) || this;
}
});
var c1 = Cat.new('Kitty');
c1.introduce(); // -> My name is Kitty
# method: BaseClass.new
var Dog = Animal.extend({
new: function Dog() {
if (!(this instanceof Dog))
return Dog.new.apply(Dog, arguments);
return Dog.super_.apply(this, arguments) || this;
},
}, {
init: function () {
console.log('Dog class init');
},
dogClassMethod: function () {
this.animalClassMethod();
console.log('Dog class method');
}
}); // -> Dog init
var d1 = Dog.new('Hachi'); // Class method new call
d1.introduce(); // -> My name is Hachi
Dog.dogClassMethod(); // -> Animal class method, Dog class method
Dog.animalClassMethod(); // -> Animal class method
## Prototype
```js
var YourClass = BaseClass.extend('YourClass');
var yourObj = YourClass.new();
// sample: JavaScript Object.defineProperty - SONICMOOV LAB
// http://lab.sonicmoov.com/development/javascript-object-defineproperty/
// or
var yourObj = new YourClass();
```
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;
## Returns
Your new object
# without BaseClass, inherits from Object, or other Classes
## inherits from Object
```js
Object.extend = BaseClass.extend;
Object.new = BaseClass.new;
var SimpleClass = Object.extend('SimpleClass', {}, {});
// or simply
var SimpleClass = BaseClass.extend.call(Object, 'SimpleClass', {}, {});
```
## inherits from EventEmitter
```js
var EventEmitter = require('events').EventEmitter;
EventEmitter.extend = BaseClass.extend;
EventEmitter.new = BaseClass.new;
var UsefulClass = EventEmitter.extend('UsefulClass', {}, {});
// or simply
var UsefulClass = BaseClass.extend.call(EventEmitter, 'UsefulClass', {}, {});
```
# EXAMPLES:
```js
var BaseClass = require('base-class-extend');
// SimpleClass
var SimpleClass = BaseClass.extend();
var s1 = new SimpleClass();
// Animal
var Animal = BaseClass.extend({
new: function Animal(name) {
if (!(this instanceof Animal))
return Animal.new.apply(Animal, arguments);
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');
}
return this._length;
},
set: function (x, y) { this._x = x; this._y = y; this._changed = true; },
});
}); // -> Animal class init
var a1 = new Animal('Annie');
a1.introduce(); // -> My name is Annie
Animal.animalClassMethod(); // -> Animal class method
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);
// Bear
var Bear = Animal.extend('Bear');
var b1 = Bear('Pooh'); // new less
b1.introduce(); // -> My name is Pooh
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;
var Cat = Animal.extend({
new: function Cat() {
if (!(this instanceof Cat))
return Cat.new.apply(Cat, arguments);
return Cat.super_.apply(this, arguments) || this;
}
return this._length;
},
set: function (x, y, z) { this._x = x; this._y = y; this._z = z; this._changed = true; },
});
});
var c1 = Cat.new('Kitty');
c1.introduce(); // -> My name is Kitty
var v3 = new Vector3D(3, 4, 5);
console.log('V3D(3, 4, 5):', v3.length);
var Dog = Animal.extend({
new: function Dog() {
if (!(this instanceof Dog))
return Dog.new.apply(Dog, arguments);
return Dog.super_.apply(this, arguments) || this;
},
}, {
init: function () {
console.log('Dog class init');
},
dogClassMethod: function () {
this.animalClassMethod();
console.log('Dog class method');
}
}); // -> Dog init
var d1 = Dog.new('Hachi'); // Class method new call
d1.introduce(); // -> My name is Hachi
Dog.dogClassMethod(); // -> Animal class method, Dog class method
Dog.animalClassMethod(); // -> Animal class method
// sample: JavaScript Object.defineProperty - SONICMOOV LAB
// http://lab.sonicmoov.com/development/javascript-object-defineproperty/
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);
```

@@ -149,0 +235,0 @@

@@ -11,2 +11,5 @@ // base-class-animal-test.js

var rex = /^[AIUEO]/i;
function a(s) { return rex.exec(s) ? 'an ' + s : 'a ' + s; }
// SimpleClass

@@ -26,15 +29,16 @@ var SimpleClass = BaseClass.extend();

introduce: function () {
console.log('My name is ' + this.name);
console.log('My name is ' + this.name + '. ' +
'I am ' + a(this.constructor.name) + '.');
},
}, {
init: function () {
console.log('Animal class init');
console.log('Animal class init. (' + this.name + ')');
},
animalClassMethod: function () {
console.log('Animal class method');
console.log('Animal class method. (' + this.name + ')');
}
}); // -> Animal class init
}); // -> Animal class init. (Animal)
var a1 = new Animal('Annie');
a1.introduce(); // -> My name is Annie
Animal.animalClassMethod(); // -> Animal class method
a1.introduce(); // -> My name is Annie. I am an Animal.
Animal.animalClassMethod(); // -> Animal class method. (Animal)

@@ -44,3 +48,3 @@ // Bear

var b1 = Bear('Pooh'); // new less
b1.introduce(); // -> My name is Pooh
b1.introduce(); // -> My name is Pooh. I am a Bear.

@@ -55,3 +59,3 @@ var Cat = Animal.extend({

var c1 = Cat.new('Kitty');
c1.introduce(); // -> My name is Kitty
c1.introduce(); // -> My name is Kitty. I am a Cat.

@@ -66,12 +70,12 @@ var Dog = Animal.extend({

init: function () {
console.log('Dog class init');
console.log('Dog class init. (' + this.name + ')');
},
dogClassMethod: function () {
this.animalClassMethod();
console.log('Dog class method');
console.log('Dog class method. (' + this.name + ')');
}
}); // -> Dog init
}); // -> Dog class init. (Dog)
var d1 = Dog.new('Hachi'); // Class method new call
d1.introduce(); // -> My name is Hachi
Dog.dogClassMethod(); // -> Animal class method, Dog class method
Dog.animalClassMethod(); // -> Animal class method
d1.introduce(); // -> My name is Hachi. I am a Dog.
Dog.dogClassMethod(); // -> Animal class method. (Animal), Dog class method. (Dog)
Dog.animalClassMethod(); // -> Animal class method. (Dog)

@@ -23,4 +23,4 @@ // base-class-test-en.js

introduce: function () {
console.log('My name is ' + this.name + '.');
console.log('I am ' + a(this.constructor.name) + '.');
console.log('My name is ' + this.name + '. ' +
'I am ' + a(this.constructor.name) + '.');
},

@@ -27,0 +27,0 @@ });

@@ -20,4 +20,4 @@ // base-class-test-jp.js

introduce: function () {
console.log('私の名前は' + this.name + 'です。');
console.log('私は' + this.constructor.name + 'です。');
console.log('私の名前は' + this.name + 'です。' +
'私は' + this.constructor.name + 'です。');
},

@@ -24,0 +24,0 @@ });

@@ -11,3 +11,3 @@ // base-class-vector-test.js

// sample: JavaScript Object.defineProperty - SONICMOOV LAB
// sample: getter/setter Object.defineProperty (JavaScript) - SONICMOOV LAB (japanese)
// http://lab.sonicmoov.com/development/javascript-object-defineproperty/

@@ -14,0 +14,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc