inherits-ex
Advanced tools
Comparing version 1.3.0 to 1.3.1
@@ -1,1 +0,18 @@ | ||
module.exports = require('xtend/mutable'); | ||
module.exports = extend; | ||
var hasOwnProperty = Object.prototype.hasOwnProperty; | ||
// var isArray = Array.isArray; | ||
function extend(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i] | ||
for (var key in source) { | ||
if (hasOwnProperty.call(source, key)) { | ||
target[key] = source[key] | ||
} | ||
} | ||
} | ||
return target | ||
} |
@@ -9,4 +9,5 @@ var setPrototypeOf = require('./setPrototypeOf'); | ||
if (aClass !== Object && aClass !== Array && aClass !== RegExp) { | ||
if (!aClass.prototype.hasOwnProperty('Class')) { | ||
defineProperty(aClass.prototype, 'Class', { | ||
var vPrototype = aClass.prototype; | ||
if (!vPrototype.hasOwnProperty('Class')) { | ||
defineProperty(vPrototype, 'Class', { | ||
value: aClass, | ||
@@ -16,13 +17,14 @@ configurable: true | ||
} | ||
if (aClass !== aClass.prototype.constructor) { | ||
aClass.prototype.constructor.apply(result, arraySlice.call(arguments, 1)); | ||
// try { | ||
// aClass.prototype.constructor.apply(result, arraySlice.call(arguments, 1)); | ||
// } catch(err) { | ||
// if (err instanceof TypeError && err.toString().lastIndexOf("invoked without 'new'") !== -1) { | ||
// result = new aClass.prototype.constructor(...arraySlice.call(arguments, 1)); | ||
// setPrototypeOf(result, aClass.prototype); | ||
// } | ||
// else throw err | ||
// } | ||
if (aClass !== vPrototype.constructor) { | ||
var args = arraySlice.call(arguments, 1); | ||
try { | ||
vPrototype.constructor.apply(result, args); | ||
} catch(err) { | ||
if (err instanceof TypeError && err.toString().lastIndexOf("invoked without 'new'") !== -1) { | ||
result = new vPrototype.constructor(...args); | ||
// console.log('TCL:: ~ file: createObject.js ~ line 24 ~ vPrototype', vPrototype, result, args); | ||
setPrototypeOf(result, vPrototype); | ||
} | ||
else throw err | ||
} | ||
} | ||
@@ -29,0 +31,0 @@ } |
var defineProperty = Object.defineProperty; | ||
var arraySlice = Array.prototype.slice; | ||
@@ -6,7 +7,8 @@ module.exports = function(aClass, aArguments) { | ||
if (aArguments) | ||
args = args.concat(Array.prototype.slice.call(aArguments)); | ||
args = args.concat(arraySlice.call(aArguments)); | ||
var result = new (Function.prototype.bind.apply(aClass, args)); | ||
if (aClass !== Object && aClass !== Array && aClass !== RegExp) { | ||
if (!aClass.prototype.hasOwnProperty('Class')) { | ||
defineProperty(aClass.prototype, 'Class', { | ||
var vPrototype = aClass.prototype; | ||
if (!vPrototype.hasOwnProperty('Class')) { | ||
defineProperty(vPrototype, 'Class', { | ||
value: aClass, | ||
@@ -16,6 +18,7 @@ configurable: true | ||
} | ||
if (aClass !== aClass.prototype.constructor) | ||
aClass.prototype.constructor.apply(result, aArguments); | ||
if (aClass !== vPrototype.constructor) { | ||
vPrototype.constructor.apply(result, aArguments); | ||
} | ||
} | ||
return result; | ||
}; |
@@ -10,3 +10,3 @@ var isEmptyFunction = require('./isEmptyFunction') | ||
var isEmpty = isEmptyFunction(result); | ||
// console.log(result.toString(), isEmpty) | ||
// console.log('getConstructor', result.toString(), isEmpty) | ||
var v = result.super_ || getPrototypeOf(result); | ||
@@ -18,3 +18,3 @@ while (isEmpty && v && v !== objectSuperCtor) { | ||
} | ||
// console.log(result.toString()) | ||
// console.log('getConstructor', result.toString()) | ||
//if (isEmpty) result = null; | ||
@@ -21,0 +21,0 @@ return result; |
@@ -8,3 +8,4 @@ var newPrototype = require('./newPrototype'); | ||
defineProperty(ctor, 'super_', superCtor); | ||
defineProperty(ctor, '__super__', superCtor.prototype);//for coffeeScirpt super keyword. | ||
defineProperty(ctor, '__super__', superCtor.prototype);//for coffeeScript super keyword. | ||
// console.log('TCL:: ~ file: inheritsDirectly.js ~ line 9 ~ ctor', ctor); | ||
ctor.prototype = newPrototype(superCtor, ctor); | ||
@@ -11,0 +12,0 @@ setPrototypeOf(ctor.prototype, superCtor.prototype); |
@@ -5,2 +5,8 @@ module.exports = function(aFunc, istanbul) { | ||
if (!result) { | ||
result = /^class\s+\S+\s*{\s*}/g.test(vStr); | ||
} | ||
if (!result) { | ||
result = /^class\s+\S+\s*{\s*(\S+\s*\n\s*)?constructor\s*\(.*\)\s*{\s*}}/g.test(vStr); | ||
} | ||
if (!result) { | ||
if (!istanbul) try {istanbul = eval("require('istanbul')");} catch(e){} | ||
@@ -7,0 +13,0 @@ if (istanbul) |
var getConstructor = require('./getConstructor'); | ||
var isEmptyFunction = require('./isEmptyFunction'); | ||
var extend = require('xtend/mutable'); | ||
var extend = require('./_extend'); | ||
@@ -13,2 +13,3 @@ module.exports = function (aClass, aConstructor) { | ||
var ctor = isEmptyFunction(aConstructor) ? getConstructor(aClass) : aConstructor; | ||
// console.log('TCL:: ~ file: newPrototype.js ~ line 13 ~ ctor', aClass, ctor); | ||
var result; | ||
@@ -36,3 +37,4 @@ if (Object.create) { //typeof Object.create === 'function' | ||
extend(result, aConstructor.prototype); | ||
// console.log('TCL:: ~ file: newPrototype.js ~ line 36 ~ result', result, aConstructor); | ||
return result; | ||
}; |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://github.com/snowyu/inherits-ex.js", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"author": { | ||
@@ -65,5 +65,3 @@ "name": "Riceball LEE", | ||
}, | ||
"dependencies": { | ||
"xtend": "^4.0.2" | ||
}, | ||
"dependencies": {}, | ||
"browser": { | ||
@@ -70,0 +68,0 @@ "./lib/isEmptyFunction.js": "./lib/isEmptyFunction-cli.js" |
@@ -1,2 +0,2 @@ | ||
### Inherits-Ex [![npm](https://img.shields.io/npm/v/inherits-ex.svg)](https://npmjs.org/package/inherits-ex) | ||
# Inherits-Ex [![npm](https://img.shields.io/npm/v/inherits-ex.svg)](https://npmjs.org/package/inherits-ex) | ||
@@ -13,3 +13,3 @@ [![Join the chat at https://gitter.im/snowyu/inherits-ex.js](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/snowyu/inherits-ex.js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | ||
[inherits](http://nodejs.org/api/util.html#util_util_inherits_constructor_superconstructor) | ||
and coffee-script. | ||
with dynamic inheritance or creation. | ||
@@ -30,9 +30,16 @@ This package modifies and enhances the standard `inherits` from node.js | ||
+ more helper functions | ||
* `isInheritedFrom(ctor, superCtor|superCtorName)` Check the ctor whether inherited from superCtor | ||
* `mixin(ctor, superCtor|superCtor[])` Mixin the methods and properties of the SuperCtor: | ||
* Clone(Copy) all superCtor's properties(methods) to ctor. | ||
* `isMixinedFrom(ctor, superCtor|superCtorName)` Whether mixined from superCtor | ||
* `createCtor(name, args, body)` Create Ctor(Class) dynamically | ||
* `createObject(ctor, args...)` Create Object instance dynamically | ||
* `createFunction(name, [args,] body[, scope[, values]])` Create Function dynamically | ||
The standard `inherits` implementation is in `inherits-ex/lib/inheritsDirectly`, | ||
of casue it's the coffee-script supports and browser-friendly. | ||
of cause it's the coffee-script supports and browser-friendly. | ||
# API | ||
## API | ||
## inherits(ctor, superCtor|superCtor[], staticInherit = true) | ||
### inherits(ctor, superCtor|superCtor[], staticInherit = true) | ||
@@ -57,3 +64,3 @@ * `staticInherit` (*boolean*): whether static inheritance,defaults to true. | ||
### usage | ||
#### usage | ||
@@ -124,3 +131,3 @@ ```coffee | ||
## inheritsDirectly(ctor, superCtor, staticInherit = true) | ||
### inheritsDirectly(ctor, superCtor, staticInherit = true) | ||
@@ -136,3 +143,3 @@ * `staticInherit` (*boolean*): whether static inheritance,defaults to true. | ||
## isInheritedFrom(ctor, superCtor|superCtorName, raiseError=false) | ||
### isInheritedFrom(ctor, superCtor|superCtorName, raiseError=false) | ||
@@ -148,3 +155,3 @@ ```js | ||
## mixin(ctor, superCtor|superCtor[], options:{ filter: number|function}) | ||
### mixin(ctor, superCtor|superCtor[], options:{ filter: number|function}) | ||
@@ -213,3 +220,3 @@ Mixin the methods and properties of the SuperCtor: Clone(Copy) all `superCtor`'s properties(methods) to ctor. | ||
## isMixinedFrom(ctor, superCtor|superCtorName) | ||
### isMixinedFrom(ctor, superCtor|superCtorName) | ||
@@ -222,3 +229,3 @@ check the ctor whether is mixined from superCtor. | ||
## createCtor(name, args, body) | ||
### createCtor(name, args, body) | ||
@@ -238,7 +245,6 @@ Create a constructor(class) dynamically. | ||
### createObject(ctor, args...) | ||
## createObject(ctor, args...) | ||
The helper function to create the object dynamically and arguments provided individually. | ||
The helper function to create the object dynamically. | ||
```js | ||
@@ -251,10 +257,29 @@ var createObject = require('inherits-ex/lib/createObject') | ||
} | ||
var o = creatObject(MyClass, 1, 2) | ||
var o = createObject(MyClass, 1, 2) | ||
console.log(o.sum) | ||
``` | ||
NOTE: DO NOT SUPPORT ES6 Class | ||
NOTE: It will call the parent constructor if the class is the Empty constructor. | ||
### usage | ||
```javascript | ||
var inherits = require('inherits-ex/lib/inherits') | ||
var createObject = require('inherits-ex/lib/createObject') | ||
class Root { | ||
constructor() { | ||
this.init = 'root' | ||
} | ||
} | ||
class MyClass { | ||
} | ||
inherits(MyClass, Root) | ||
var obj = createObject(MyClass) | ||
assert.equal(obj.init, 'root') | ||
``` | ||
Usage: | ||
```coffee | ||
@@ -270,21 +295,20 @@ | ||
obj = createObject(MyObject, "a", "b") | ||
#obj = new MyObject("a", "b") # it will have no property a and b. | ||
# obj = new MyObject("a", "b") # it will have no property a and b. | ||
assert.equal obj.a "a" | ||
assert.equal obj.b "b" | ||
``` | ||
### createObjectWith(ctor, [args...]) | ||
``` | ||
## createObjectWith(ctor, [args...]) | ||
The helper function to create the object dynamically. provides the arguments as an array (or an array-like object). | ||
The helper function to create the object dynamically. | ||
```js | ||
var createObjectWith = require('inherits-ex/lib/createObjectWith') | ||
var createObjectWith = require('inherits-ex/lib/createObjectWith') | ||
var obj = createObjectWith(MyObject, ['a', 'b']) | ||
``` | ||
NOTE: DO NOT SUPPORT ES6 Class | ||
NOTE: It will call the parent constructor if the class is the Empty constructor. | ||
### createFunction(name, [args,] body[, scope[, values]]) | ||
## createFunction(name, [args,] body[, scope[, values]]) | ||
* arguments: | ||
@@ -305,3 +329,3 @@ * `name` *(String)*: the function name | ||
### usage | ||
Usage: | ||
@@ -308,0 +332,0 @@ ```coffee |
@@ -1,1 +0,18 @@ | ||
module.exports = require('xtend/mutable'); | ||
module.exports = extend; | ||
var hasOwnProperty = Object.prototype.hasOwnProperty; | ||
// var isArray = Array.isArray; | ||
function extend(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i] | ||
for (var key in source) { | ||
if (hasOwnProperty.call(source, key)) { | ||
target[key] = source[key] | ||
} | ||
} | ||
} | ||
return target | ||
} |
@@ -9,4 +9,5 @@ var setPrototypeOf = require('./setPrototypeOf'); | ||
if (aClass !== Object && aClass !== Array && aClass !== RegExp) { | ||
if (!aClass.prototype.hasOwnProperty('Class')) { | ||
defineProperty(aClass.prototype, 'Class', { | ||
var vPrototype = aClass.prototype; | ||
if (!vPrototype.hasOwnProperty('Class')) { | ||
defineProperty(vPrototype, 'Class', { | ||
value: aClass, | ||
@@ -16,13 +17,14 @@ configurable: true | ||
} | ||
if (aClass !== aClass.prototype.constructor) { | ||
aClass.prototype.constructor.apply(result, arraySlice.call(arguments, 1)); | ||
// try { | ||
// aClass.prototype.constructor.apply(result, arraySlice.call(arguments, 1)); | ||
// } catch(err) { | ||
// if (err instanceof TypeError && err.toString().lastIndexOf("invoked without 'new'") !== -1) { | ||
// result = new aClass.prototype.constructor(...arraySlice.call(arguments, 1)); | ||
// setPrototypeOf(result, aClass.prototype); | ||
// } | ||
// else throw err | ||
// } | ||
if (aClass !== vPrototype.constructor) { | ||
var args = arraySlice.call(arguments, 1); | ||
try { | ||
vPrototype.constructor.apply(result, args); | ||
} catch(err) { | ||
if (err instanceof TypeError && err.toString().lastIndexOf("invoked without 'new'") !== -1) { | ||
result = new vPrototype.constructor(...args); | ||
// console.log('TCL:: ~ file: createObject.js ~ line 24 ~ vPrototype', vPrototype, result, args); | ||
setPrototypeOf(result, vPrototype); | ||
} | ||
else throw err | ||
} | ||
} | ||
@@ -29,0 +31,0 @@ } |
var defineProperty = Object.defineProperty; | ||
var arraySlice = Array.prototype.slice; | ||
@@ -6,7 +7,8 @@ module.exports = function(aClass, aArguments) { | ||
if (aArguments) | ||
args = args.concat(Array.prototype.slice.call(aArguments)); | ||
args = args.concat(arraySlice.call(aArguments)); | ||
var result = new (Function.prototype.bind.apply(aClass, args)); | ||
if (aClass !== Object && aClass !== Array && aClass !== RegExp) { | ||
if (!aClass.prototype.hasOwnProperty('Class')) { | ||
defineProperty(aClass.prototype, 'Class', { | ||
var vPrototype = aClass.prototype; | ||
if (!vPrototype.hasOwnProperty('Class')) { | ||
defineProperty(vPrototype, 'Class', { | ||
value: aClass, | ||
@@ -16,6 +18,7 @@ configurable: true | ||
} | ||
if (aClass !== aClass.prototype.constructor) | ||
aClass.prototype.constructor.apply(result, aArguments); | ||
if (aClass !== vPrototype.constructor) { | ||
vPrototype.constructor.apply(result, aArguments); | ||
} | ||
} | ||
return result; | ||
}; |
@@ -10,3 +10,3 @@ var isEmptyFunction = require('./isEmptyFunction') | ||
var isEmpty = isEmptyFunction(result); | ||
// console.log(result.toString(), isEmpty) | ||
// console.log('getConstructor', result.toString(), isEmpty) | ||
var v = result.super_ || getPrototypeOf(result); | ||
@@ -18,3 +18,3 @@ while (isEmpty && v && v !== objectSuperCtor) { | ||
} | ||
// console.log(result.toString()) | ||
// console.log('getConstructor', result.toString()) | ||
//if (isEmpty) result = null; | ||
@@ -21,0 +21,0 @@ return result; |
@@ -8,3 +8,4 @@ var newPrototype = require('./newPrototype'); | ||
defineProperty(ctor, 'super_', superCtor); | ||
defineProperty(ctor, '__super__', superCtor.prototype);//for coffeeScirpt super keyword. | ||
defineProperty(ctor, '__super__', superCtor.prototype);//for coffeeScript super keyword. | ||
// console.log('TCL:: ~ file: inheritsDirectly.js ~ line 9 ~ ctor', ctor); | ||
ctor.prototype = newPrototype(superCtor, ctor); | ||
@@ -11,0 +12,0 @@ setPrototypeOf(ctor.prototype, superCtor.prototype); |
@@ -5,2 +5,8 @@ module.exports = function(aFunc, istanbul) { | ||
if (!result) { | ||
result = /^class\s+\S+\s*{\s*}/g.test(vStr); | ||
} | ||
if (!result) { | ||
result = /^class\s+\S+\s*{\s*(\S+\s*\n\s*)?constructor\s*\(.*\)\s*{\s*}}/g.test(vStr); | ||
} | ||
if (!result) { | ||
if (!istanbul) try {istanbul = eval("require('istanbul')");} catch(e){} | ||
@@ -7,0 +13,0 @@ if (istanbul) |
var getConstructor = require('./getConstructor'); | ||
var isEmptyFunction = require('./isEmptyFunction'); | ||
var extend = require('xtend/mutable'); | ||
var extend = require('./_extend'); | ||
@@ -13,2 +13,3 @@ module.exports = function (aClass, aConstructor) { | ||
var ctor = isEmptyFunction(aConstructor) ? getConstructor(aClass) : aConstructor; | ||
// console.log('TCL:: ~ file: newPrototype.js ~ line 13 ~ ctor', aClass, ctor); | ||
var result; | ||
@@ -36,3 +37,4 @@ if (Object.create) { //typeof Object.create === 'function' | ||
extend(result, aConstructor.prototype); | ||
// console.log('TCL:: ~ file: newPrototype.js ~ line 36 ~ result', result, aConstructor); | ||
return result; | ||
}; |
@@ -41,3 +41,3 @@ var chai = require('chai') | ||
constructor(inited="Root", other) { | ||
this.inited= this.inited | ||
this.inited= inited | ||
this.other = other | ||
@@ -72,2 +72,15 @@ return "Root" | ||
it("test inherits and call super parent class", function() { | ||
class B { | ||
constructor() { | ||
var result = new this.constructor.super_() | ||
return result; | ||
} | ||
} | ||
inherits(B, A1) | ||
var obj = new B | ||
assert.equal(obj.inited, 'A1') | ||
}) | ||
it("test inherits with none static inheritance", function() { | ||
@@ -271,3 +284,3 @@ class R{ | ||
}) | ||
it.skip('should call the parent\'s constructor method if it no constructor', function(){ | ||
it('should call the parent\'s constructor method if it no constructor', function(){ | ||
function A12() {} | ||
@@ -283,3 +296,3 @@ assert.equal(inherits(A12, A1), true) | ||
}) | ||
it.skip('should call the root\'s constructor method if its parent no constructor yet', function(){ | ||
it('should call the root\'s constructor method if its parent no constructor yet', function(){ | ||
//ES6 Class can not supports this. | ||
@@ -286,0 +299,0 @@ //the class X defined, toString should be 'class X{}', not 'function X(){}' |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
117690
0
2087
336
- Removedxtend@^4.0.2
- Removedxtend@4.0.2(transitive)