Requirement: suddenly need to masturbate the SDK, according to the large function group divided into small function module JS.
But the export should be unified as an entry.
Hence, the planeCopy the code
The extends of the es6 default class cannot inherit more than one. It just can’t be direct
class A extends B.C{}
Copy the code
So, use simple mixins to do a dirty operation
Here’s how node is written (not import). I’m using require
Js: entrance index. Js
'use strict'
const A = require('./A.js')
const B = require('./B.js')
const C = require('./C.js')
const copyProperties = (target,source) = >{ // add a copy function to copy all the static classes and their prototypes passed in.
Object.getOwnpropertyNames(source).concat(Object.getOwnpropertySymbols(source)).forEach((prop) = >{
// Filter conditions
if(! prop.match(/^(? :constructor|protype|arguments|name))) {Object.defineProperty(target, prop, Object.getOwnPropertyDescriptor(source, prop))
}
})
}
// Create a Mixins body
const Mixins = (BaseClass, ... mixins) = >{
// Create a Base Base. Bind other mixins to it
class Base extends BaseClass{
constructor(. props){
super(... props) } }// Bind the remaining classes that need to be inherited to Base. The class and its prototype
mixins.forEach((mixin) = > {
copyProperties(Base, mixin)
copyProperties(Base.prototype, mixin.prototype)
})
return Base
}
class Demo extends Mixins(A.B.C// Export the whole function,DemoContains otherclassMethods and their properties inmodule.exports = Demo
Copy the code
A.js
class A extends Some{
constructor(config) {
super(config)
}
}
module.exports = A
Copy the code
B.js
class B{}
module.exports = B
Copy the code
C.js
class C{}
module.exports = C
Copy the code
A.js, B.js, C.js content is not important…
If I write it in JS. Just change the above node notation.
If you’re googling, nodejs-xxx.com can block links you don’t want to see coming up.
Call it a day.
This article is for reference only.Copy the code