rse / aggregation Goto Github PK
View Code? Open in Web Editor NEWAggregation of Base Class and Mixin Classes
Aggregation of Base Class and Mixin Classes
Just think that ES6 is not trying to use Reflect
?
Modified the code
var aggregation = (base, ...mixins) => {
/* create aggregation class */
let aggregate = class __Aggregate extends base {
constructor (...args) {
/* call base class constructor */
super(...args)
/* call mixin's initializer */
mixins.forEach(mixin => {
if (typeof mixin.prototype.initializer === 'function') { mixin.prototype.initializer.apply(this, args) }
})
}
}
/* copy properties */
let copyProps = (target, source) => {
Reflect.ownKeys(source)
.forEach(prop => {
if (
prop.match(
/^(?:initializer|constructor|prototype|arguments|caller|name|bind|call|apply|toString|length)$/
)
) { return }
Reflect.set(
target,
prop,
Reflect.getOwnPropertyDescriptor(source, prop)
)
})
}
/* copy all properties of all mixins into aggregation class */
mixins.forEach(mixin => {
copyProps(aggregate.prototype, mixin.prototype)
copyProps(aggregate, mixin)
})
return aggregate
}
Finally, I feel that Reflect is a bit more interesting, nothing more. o((^โฝ^))o
I am using the nodejs event emitter as a mixin and the in the latest version of nodejs 12.16 they haved added a Symbol property which breaks aggregation.
if I add add a console.log(prop) to the forEach loop the output is below.
You can see it throws an error when encountering a Symbol which is does have a match method because it is not a string.
I have no idea if these this new Symbol must be mixed in order for the Emitter class to work correctly but if so I have no idea of how to the do that properly.
constructor
_events
_eventsCount
_maxListeners
setMaxListeners
getMaxListeners
emit
addListener
on
prependListener
once
prependOnceListener
removeListener
off
removeAllListeners
listeners
rawListeners
listenerCount
eventNames
Symbol(kCapture)
/mnt/AllData/hacking/active-dev-repos/uci/lib/uci-utils/sync/node_modules/aggregation/src/aggregation-es6.js:1
TypeError: prop.match is not a function
at /mnt/AllData/hacking/active-dev-repos/uci/lib/uci-utils/sync/node_modules/aggregation/src/aggregation-es6.js:50:18
at Array.forEach (<anonymous>)
at copyProps (/mnt/AllData/hacking/active-dev-repos/uci/lib/uci-utils/sync/node_modules/aggregation/src/aggregation-es6.js:48:8)
at /mnt/AllData/hacking/active-dev-repos/uci/lib/uci-utils/sync/node_modules/aggregation/src/aggregation-es6.js:58:5
at Array.forEach (<anonymous>)
at aggregation (/mnt/AllData/hacking/active-dev-repos/uci/lib/uci-utils/sync/node_modules/aggregation/src/aggregation-es6.js:57:10)
at Object.<anonymous> (/mnt/AllData/hacking/active-dev-repos/uci/lib/uci-utils/sync/src/sync.js:19:20)
at Generator.next (<anonymous>)
`class Colored {
initializer () { this._color = "white" }
get color () { return this._color }
set color (v) { this._color = v }
}
class ZCoord {
initializer () { this._z = 0 }
get z () { return this._z }
set z (v) { this._z = v }
}
class Shape {
constructor (x, y) { this._x = x; this._y = y }
get x () { return this._x }
set x (v) { this._x = v }
get y () { return this._y }
set y (v) { this._y = v }
}
class Rectangle extends aggregation(Shape, Colored, ZCoord) {}
new Rectangle(5,6)`
Uncaught TypeError: Class constructor Shape cannot be invoked without 'new' at new __Aggregate (aggregation-es6.js:31) __Aggregate @ aggregation-es6.js:31
is it possible to create a typed version?
I found this on the es6examples site!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.