拆分1
- a.js
const _a = Symbol('a')
const _b = Symbol('b')
const _options = Symbol('options')
module.exports = CIR => class A extends CIR {
constructor (options) {
super(options)
this[_options] = {
name: 'A',
...options
}
}
b () {
this[_a]()
this[_b]()
}
[_a] () {
console.log(1)
return 1
}
[_b] () {
console.log(2)
return 2
}
}
拆分2
- b.js
const _a = Symbol('a')
const _b = Symbol('b')
const _options = Symbol('options')
module.exports = CIR => class B extends CIR {
constructor (options) {
super(options)
this[_options] = {
name: 'B',
...options
}
}
b () {
this[_a]()
this[_b]()
}
[_a] () {
console.log(3)
return 3
}
[_b] () {
console.log(4)
return 4
}
}
汇总
- index.js
const list = [
require('./a'),
require('./b')
]
const Base = list.reduce((a, b) => b(a), class B {})
class Case extends Base {
constructor (options) {
super(options)
this.options = options
}
}
const ca = new Case({})
console.log(ca)
输出
G:\workspaces\NODE\beauty>node test.js
Case {
options: {},
[Symbol(options)]: { name: 'A' },
[Symbol(options)]: { name: 'B' }
}
网友评论