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.12 to 0.0.13

116

lib/base-class-extend.js

@@ -43,6 +43,6 @@ // base-class-extend.js

// BaseClass.extend
// Base.extend
// Usage:
// var SimpleClass =
// BaseClass.extend(
// Base.extend(
// {new: function SimpleClass() {

@@ -55,6 +55,6 @@ // SimpleClass.super_.call(this);

// {classMethod1: function () {}});
function BaseClass_extend(name, proto, classProps) {
function Base_extend(name, proto, staticProps) {
// check argument: name
if (typeof name !== 'string') {
classProps = proto;
staticProps = proto;
proto = name;

@@ -68,5 +68,4 @@ name = '';

var ctor = proto.hasOwnProperty('constructor') ? proto.constructor :
proto.hasOwnProperty('ctor') ? proto.ctor :
proto.hasOwnProperty('new') ? proto.new :
Function('proto, superCtor, BaseClass_create',
proto.hasOwnProperty('new') ? proto['new'] :
Function('proto, superCtor, Base_create',
'return function ' + name + '() {\n' +

@@ -77,6 +76,6 @@ ' "use strict";' +

' this instanceof Error && !this.hasOwnProperty("message"))\n' +
' return BaseClass_create.apply(proto.constructor, arguments);\n' +
' return Base_create.apply(proto.constructor, arguments);\n' +
' if (superCtor !== Object && superCtor !== Array && superCtor !== Error)\n' +
' superCtor.apply(this, arguments); }')
(proto, superCtor, BaseClass_create);
(proto, superCtor, Base_create);
if (typeof ctor !== 'function')

@@ -86,3 +85,3 @@ throw new TypeError('constructor must be a function');

ctor.prototype = proto;
ctor = Function('proto, ctor, BaseClass_create',
ctor = Function('proto, ctor, Base_create',
'return function ' + name + '() {\n' +

@@ -93,5 +92,5 @@ ' "use strict";' +

' this instanceof Error && !this.hasOwnProperty("message"))\n' +
' return BaseClass_create.apply(proto.constructor, arguments);\n' +
' return Base_create.apply(proto.constructor, arguments);\n' +
' ctor.apply(this, arguments); }')
(proto, ctor, BaseClass_create);
(proto, ctor, Base_create);
}

@@ -101,4 +100,3 @@ ctor.prototype = proto;

// override constructor
delete proto.ctor;
delete proto.new;
delete proto['new'];
setValue(proto, 'constructor', ctor);

@@ -110,24 +108,24 @@

// constructor.__proto__ -> for inherits class methods
if (classProps == null || typeof classProps !== 'object') {
setProto(ctor, superCtor);
if (staticProps == null || typeof staticProps !== 'object') {
setProto(ctor, superCtor === Object ? Function.prototype : superCtor);
}
else {
setProto(ctor, classProps);
setProto(classProps, superCtor);
setProto(ctor, staticProps);
setProto(staticProps, superCtor === Object ? Function.prototype : superCtor);
// class initializer: init
var init = classProps.hasOwnProperty('init') && classProps.init;
delete classProps.init;
var init = staticProps.hasOwnProperty('init') && staticProps.init;
delete staticProps.init;
if (typeof init === 'function') init.call(ctor);
// add name to methods/functions if not found
var keys = Object.keys(classProps);
var keys = Object.keys(staticProps);
for (var i = 0, n = keys.length; i < n; ++i) {
var key = keys[i];
if (typeof classProps[key] === 'function' &&
!classProps[key].name) {
classProps[key] = Function('fn',
if (typeof staticProps[key] === 'function' &&
!staticProps[key].name) {
staticProps[key] = Function('fn',
'return function ' + key + '_() {\n' +
' return fn.apply(this, arguments); }')
(classProps[key]);
(staticProps[key]);
}

@@ -138,16 +136,9 @@ }

// add methods and class methods if not found (in prototype chain)
if (ctor.extend !== BaseClass_extend) ctor.extend = BaseClass_extend;
if (ctor.create !== BaseClass_create) ctor.create = BaseClass_create;
if (ctor.new !== BaseClass_create) ctor.new = BaseClass_create;
if (ctor.extend !== Base_extend) ctor.extend = Base_extend;
if (ctor.create !== Base_create) ctor.create = Base_create;
if (ctor['new'] !== Base_create) ctor['new'] = Base_create;
if (!('constructors' in ctor))
Object.defineProperty(ctor, 'constructors',
Object.getOwnPropertyDescriptor(getProto(BaseClass), 'constructors'));
if (!('private' in proto)) proto['private'] = Base_addPrototype;
if (!('addPrototype' in proto)) proto['addPrototype'] = Base_addPrototype;
if (!('private' in proto)) proto.private = BaseClass_private;
if (!('constructors' in proto))
Object.defineProperty(proto, 'constructors',
Object.getOwnPropertyDescriptor(BaseClass.prototype, 'constructors'));
// constructor.super_ -> for points super class

@@ -160,12 +151,10 @@ setConst(ctor, 'super_', superCtor);

// BaseClass.new or BaseClass.create
function BaseClass_create() {
//assert(this === this.prototype.constructor,
// 'prototype of class/constructor is not class/constructor');
// Base.new or Base.create
function Base_create() {
if (this.prototype instanceof Array) {
var obj = Array.apply(null, arguments); // [] or new Array
var obj = Array.apply(null, arguments);
setProto(obj, this.prototype);
}
else if (this.prototype instanceof Error) {
var obj = Error.apply(null, arguments); // new Error
var obj = Error.apply(null, arguments);
if (!obj.hasOwnProperty('message') &&

@@ -176,3 +165,3 @@ typeof arguments[0] === 'string')

obj.stack = obj.stack.split('\n').filter(function (str) {
return !/((base-class.js)|(BaseClass_create))/.test(str);
return !/((base-class.js)|(Base_create))/.test(str);
}).join('\n');

@@ -183,8 +172,3 @@ setProto(obj, this.prototype);

var obj = Object.create(this.prototype);
//assert(obj.constructor === this, 'new object is not instance of class (constructor)');
return this.apply(obj, arguments), obj;
//var res = this.apply(obj, arguments) || obj;
//assert(res.constructor === this, 'constructor returns other class object');
//assert(res === obj, 'constructor returns other object');
//return obj;
}

@@ -196,4 +180,4 @@

// BaseClass_private
function BaseClass_private(proto) {
// Base_addPrototype
function Base_addPrototype(proto) {
setProto(proto, getProto(this));

@@ -204,22 +188,8 @@ setProto(this, proto);

var BaseClass = BaseClass_extend('BaseClass',
{private: BaseClass_private,
get constructors() {
var ctors = [], obj = this;
while (obj) {
if (obj.hasOwnProperty('constructor'))
ctors.push(obj.constructor);
obj = getProto(obj); }
return ctors; }},
{extend: BaseClass_extend,
create: BaseClass_create,
new: BaseClass_create,
get constructors() {
var ctors = [], ctor = this;
while (ctor) {
if (typeof ctor === 'function' &&
ctor.hasOwnProperty('prototype'))
ctors.push(ctor);
ctor = getProto(ctor); }
return ctors; }});
var Base = Base_extend('Base',
{'private': Base_addPrototype,
addPrototype : Base_addPrototype},
{extend: Base_extend,
create: Base_create,
'new': Base_create});

@@ -229,9 +199,9 @@

if (typeof module !== 'undefined') {
module.exports = exports = BaseClass;
module.exports = exports = Base;
}
else {
var g = Function('return this')();
g.BaseClass = BaseClass;
g.BaseClass = Base;
}
})();
{
"name": "base-class-extend",
"version": "0.0.12",
"version": "0.0.13",
"description": "Base Class constructor for easy class definition - supports getter/setter, inherit/extend Array Error or EventEmitter etc",
"main": "lib/base-class-extend.js",
"devDependencies": {
"get-constructors": ">=0.0.3"
},
"keywords": [

@@ -7,0 +10,0 @@ "Array",

@@ -42,3 +42,3 @@ [base-class-extend](https://www.npmjs.org/package/base-class-extend) - npm

## メソッド: Class.extend(name, proto, classProps)
## メソッド: Class.extend(name, proto, staticProps)

@@ -50,4 +50,4 @@ 基底クラスを継承した新しいクラス(コンストラクタ関数)を定義します。

```js
var YourClass = BaseClass.extend([name], [proto], [classProps]);
var YourSubClass = YourClass.extend([name], [proto], [classProps]);
var YourClass = BaseClass.extend([name], [proto], [staticProps]);
var YourSubClass = YourClass.extend([name], [proto], [staticProps]);
```

@@ -60,7 +60,7 @@

+ **proto**: 新しいクラスのプロトタイプオブジェクト (省略可)
+ **new**, **ctor** または **constructor**: コンストラクタ関数 (省略可)
+ **new** または **constructor**: コンストラクタ関数 (省略可)
+ **get** prop(): getter関数 (省略可)
+ **set** prop(value): setter関数 (省略可)
+ **any methods**: メソッドまたはメンバー関数 (省略可)
+ **classProps**: クラス/静的プロパティのオブジェクト (省略可)
+ **staticProps**: クラス/静的プロパティのオブジェクト (省略可)
+ **init**: 初期化関数 (省略可)

@@ -71,4 +71,4 @@ + **get** prop(): getter関数 (省略可)

※**proto**を省略した場合**classProps**も省略する必要がある<br/>
※**classProps**を指定したい場合、省略したい**proto**の部分は`{}`と指定すると良い
※**proto**を省略した場合**staticProps**も省略する必要がある<br/>
※**staticProps**を指定したい場合、省略したい**proto**の部分は`{}`と指定すると良い

@@ -198,3 +198,3 @@ ### 返り値

## メソッド: this.private(proto)
## メソッド: this.addPrototype(proto)

@@ -209,3 +209,3 @@ プライベート変数または隠された変数を定義できます。<br/>

var private1;
this.private({
this.addPrototype({
method1: function method1() {

@@ -238,3 +238,3 @@ console.log(private1); },

var private2 = 'abc'; // getter経由のアクセス, setter無し
this.private({
this.addPrototype({
get private1() { return private1; }, // getter

@@ -248,5 +248,6 @@ set private1(val) { private1 = val; }, // setter

## プロパティ: this.constructor
## メソッド: this.private(proto)
コンストラクタ関数(クラス)を取得する。
プライベート変数または隠された変数を定義できます。<br/>
getter/setterやプライベート変数をアクセスできる通常のメソッドをサポートします。

@@ -256,48 +257,41 @@ ### 形式

```js
var MyClass = BaseClass.extend('MyClass');
var o1 = new MyClass();
console.log(o1.constructor === MyClass); // -> true
// 'new'メソッドまたは'constructor'関数の中に定義すること
var private1;
this.private({
method1: function method1() {
console.log(private1); },
get prop1() { return private1; },
set prop1(val) { private1 = val; },
});
```
### 返り値
### パラメータ
コンストラクタ関数(クラス)。
+ **proto**: プライベート変数にアクセスできるメソッドが含まれるプロトタイプオブジェクト (必須)
+ **get** prop(): getter関数 (省略可)
+ **set** prop(value): setter関数 (省略可)
+ **any methods**: メソッドまたはメンバー関数 (省略可)
## プロパティ: this.constructors
### 返り値
コンストラクタ関数(クラス)の配列を取得する。
渡したプロトタイプオブジェクト。
### 形式
### 詳細
```js
var MyClass = BaseClass.extend('MyClass');
var o1 = new MyClass();
var classes = o1.constructors;
console.log(classes[0] === MyClass); // -> true
console.log(classes[1] === BaseClass); // -> true
console.log(classes[2] === Object); // -> true
```
サンプル:
## 返り値
コンストラクタ関数(クラス)の配列。
## プロパティ: Class.constructors
コンストラクタ関数(クラス)の配列を取得する。
### 形式
```js
var MyClass = BaseClass.extend('MyClass');
var classes = MyClass.constructors;
console.log(classes[0] === MyClass); // -> true
console.log(classes[1] === BaseClass); // -> true
console.log(classes[2] === Object); // -> true
var YourClass = BaseClass.extend({
new: function YourClass() {
var private1 = 123; // getter/setter経由のアクセス
var private2 = 'abc'; // getter経由のアクセス, setter無し
this.private({
get private1() { return private1; }, // getter
set private1(val) { private1 = val; }, // setter
get private2() { return private2; }, // getter
});
},
});
```
## 返り値
コンストラクタ関数(クラス)の配列。
# 使用例:

@@ -432,4 +426,8 @@

# 参考:
## [get-constructors](https://www.npmjs.org/package/get-constructors) - npm
# ライセンス:
MIT

@@ -41,3 +41,3 @@ [base-class-extend](https://www.npmjs.org/package/base-class-extend) - npm

## method: Class.extend(name, proto, classProps)
## method: Class.extend(name, proto, staticProps)

@@ -49,4 +49,4 @@ Define new class (constructor function) that inherited from Base Class.

```js
var YourClass = BaseClass.extend([name], [proto], [classProps]);
var YourSubClass = YourClass.extend([name], [proto], [classProps]);
var YourClass = BaseClass.extend([name], [proto], [staticProps]);
var YourSubClass = YourClass.extend([name], [proto], [staticProps]);
```

@@ -59,7 +59,7 @@

+ **proto**: the prototype object for your class, optional
+ **new**, **ctor** or **constructor**: constructor function, 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
+ **classProps**: the object for class or static properties, optional
+ **staticProps**: the object for class or static properties, optional
+ **init**: initialize function, optional

@@ -70,4 +70,4 @@ + **get** prop(): getter function, optional

You have to omit **classProps** also, if you omit **proto**.<br/>
You have to specify **proto** or `{}`, if you want to specify **classProps**.
You have to omit **staticProps** also, if you omit **proto**.<br/>
You have to specify **proto** or `{}`, if you want to specify **staticProps**.

@@ -197,3 +197,3 @@ ### Returns

## method: this.private(proto)
## method: this.addPrototype(proto)

@@ -208,3 +208,3 @@ You can define private variables, hidden variables.<br/>

var private1;
this.private({
this.addPrototype({
method1: function method1() {

@@ -237,3 +237,3 @@ console.log(private1); },

var private2 = 'abc'; // access via getter, no setter
this.private({
this.addPrototype({
get private1() { return private1; }, // getter

@@ -247,5 +247,6 @@ set private1(val) { private1 = val; }, // setter

## property: this.constructor
## method: this.private(proto)
Get constructor function. (Class)
You can define private variables, hidden variables.<br/>
Also support getter/setter, and normal methods to access private variables.

@@ -255,48 +256,41 @@ ### Format

```js
var MyClass = BaseClass.extend('MyClass');
var o1 = new MyClass();
console.log(o1.constructor === MyClass); // -> true
// defined in 'new' method or 'constructor' function
var private1;
this.private({
method1: function method1() {
console.log(private1); },
get prop1() { return private1; },
set prop1(val) { private1 = val; },
});
```
### Returns
### Parameters
The constructor function. (Class)
+ **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
## property: this.constructors
### Returns
Get an array of constructor functions. (Classes)
The prototype object you passed.
### Format
### Details
```js
var MyClass = BaseClass.extend('MyClass');
var o1 = new MyClass();
var classes = o1.constructors;
console.log(classes[0] === MyClass); // -> true
console.log(classes[1] === BaseClass); // -> true
console.log(classes[2] === Object); // -> true
```
Sample:
## Returns
An array of constructor functions. (Classes)
## property: Class.constructors
Get an array of constructor functions.
### Format
```js
var MyClass = BaseClass.extend('MyClass');
var classes = MyClass.constructors;
console.log(classes[0] === MyClass); // -> true
console.log(classes[1] === BaseClass); // -> true
console.log(classes[2] === Object); // -> true
var YourClass = BaseClass.extend({
new: 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
});
},
});
```
## Returns
An array of constructor functions.
# EXAMPLES:

@@ -431,4 +425,8 @@

# SEE ALSO:
## [get-constructors](https://www.npmjs.org/package/get-constructors) - npm
# LICENSE:
MIT
// animal-test.js
'use strict';
var constructors = require('get-constructors');
constructors.extendPrototype();
try {

@@ -81,14 +84,14 @@ var BaseClass = require('../lib/base-class-extend');

function mapName(elem) { return elem.name; }
console.log(a1.constructors.map(mapName).join(' < '));
console.log(Animal.constructors.map(mapName).join(' < '));
console.log(b1.constructors.map(mapName).join(' < '));
console.log(Bear.constructors.map(mapName).join(' < '));
console.log(c1.constructors.map(mapName).join(' < '));
console.log(Cat.constructors.map(mapName).join(' < '));
console.log(d1.constructors.map(mapName).join(' < '));
console.log(Dog.constructors.map(mapName).join(' < '));
console.log('a1 ', a1.constructors.map(mapName).join(' < '));
console.log('Animal', Animal.constructors.map(mapName).join(' < '));
console.log('b1 ', b1.constructors.map(mapName).join(' < '));
console.log('Bear ', Bear.constructors.map(mapName).join(' < '));
console.log('c1 ', c1.constructors.map(mapName).join(' < '));
console.log('Cat ', Cat.constructors.map(mapName).join(' < '));
console.log('d1 ', d1.constructors.map(mapName).join(' < '));
console.log('Dog ', Dog.constructors.map(mapName).join(' < '));
var Klass = BaseClass.extend.call(Object, 'Klass');
var k1 = new Klass();
console.log(Klass.constructors.map(mapName).join(' < '));
console.log(k1.constructors.map(mapName).join(' < '));
console.log('k1 ', k1.constructors.map(mapName).join(' < '));
console.log('Klass ', Klass.constructors.map(mapName).join(' < '));
// function-test.js
'use strict';
var constructors = require('get-constructors');
constructors.extendPrototype();
try {

@@ -5,0 +8,0 @@ var BaseClass = require('../lib/base-class-extend');

// object-test.js
'use strict';
var constructors = require('get-constructors');
constructors.extendPrototype();
try {

@@ -5,0 +8,0 @@ var BaseClass = require('../lib/base-class-extend');

// private-test.js
'use strict';
var constructors = require('get-constructors');
constructors.extendPrototype();
try {

@@ -59,6 +62,6 @@ var BaseClass = require('../lib/base-class-extend');

function mapName(elem) { return elem.name; }
console.log(p1.constructors.map(mapName).join(' < '));
console.log(p2.constructors.map(mapName).join(' < '));
console.log(p3.constructors.map(mapName).join(' < '));
console.log(p4.constructors.map(mapName).join(' < '));
console.log(PrivateClass1.constructors.map(mapName).join(' < '));
console.log('p1 ', p1.constructors.map(mapName).join(' < '));
console.log('p2 ', p2.constructors.map(mapName).join(' < '));
console.log('p3 ', p3.constructors.map(mapName).join(' < '));
console.log('p4 ', p4.constructors.map(mapName).join(' < '));
console.log('PrivateClass1', PrivateClass1.constructors.map(mapName).join(' < '));
// quick.js
'use strict';
var constructors = require('get-constructors');
constructors.extendPrototype();
try {

@@ -36,3 +39,3 @@ var BaseClass = require('../lib/base-class-extend');

function mapName(elem) { return elem.name; }
console.log(myObj.constructors.map(mapName).join(' < '));
console.log(MyClass.constructors.map(mapName).join(' < '));
console.log('myObj ', myObj.constructors.map(mapName).join(' < '));
console.log('MyClass', MyClass.constructors.map(mapName).join(' < '));
// test-en.js
'use strict';
var constructors = require('get-constructors');
constructors.extendPrototype();
try {

@@ -5,0 +8,0 @@ var BaseClass = require('../lib/base-class-extend');

// test-jp.js
'use strict';
var constructors = require('get-constructors');
constructors.extendPrototype();
try {

@@ -5,0 +8,0 @@ var BaseClass = require('../lib/base-class-extend');

// vector-private-test.js
'use strict';
var constructors = require('get-constructors');
constructors.extendPrototype();
try {

@@ -75,5 +78,5 @@ var BaseClass = require('../lib/base-class-extend');

function mapName(elem) { return elem.name; }
console.log(v2.constructors.map(mapName).join(' < '));
console.log(Vector2D.constructors.map(mapName).join(' < '));
console.log(v3.constructors.map(mapName).join(' < '));
console.log(Vector3D.constructors.map(mapName).join(' < '));
console.log('v2 ', v2.constructors.map(mapName).join(' < '));
console.log('Vector2D', Vector2D.constructors.map(mapName).join(' < '));
console.log('v3 ', v3.constructors.map(mapName).join(' < '));
console.log('Vector3D', Vector3D.constructors.map(mapName).join(' < '));
// vector-test.js
'use strict';
var constructors = require('get-constructors');
constructors.extendPrototype();
try {

@@ -64,5 +67,5 @@ var BaseClass = require('../lib/base-class-extend');

function mapName(elem) { return elem.name; }
console.log(v2.constructors.map(mapName).join(' < '));
console.log(Vector2D.constructors.map(mapName).join(' < '));
console.log(v3.constructors.map(mapName).join(' < '));
console.log(Vector3D.constructors.map(mapName).join(' < '));
console.log('v2 ', v2.constructors.map(mapName).join(' < '));
console.log('Vector2D', Vector2D.constructors.map(mapName).join(' < '));
console.log('v3 ', v3.constructors.map(mapName).join(' < '));
console.log('Vector3D', Vector3D.constructors.map(mapName).join(' < '));

Sorry, the diff of this file is not supported yet

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