标签:为我 res 过程 返回 符号 技术 fir 包括 这不
function mixin (behaviour, sharedBehaviour = {}) { const instanceKeys = Reflect.ownKeys(behaviour); const sharedKeys = Reflect.ownKeys(sharedBehaviour); const typeTag = Symbol(‘isa‘); function _mixin (target) { for (let property of instanceKeys) Object.defineProperty(target, property, { value: behaviour[property] }); Object.defineProperty(target, typeTag, { value: true }); return target; } for (let property of sharedKeys) Object.defineProperty(_mixin, property, { value: sharedBehaviour[property], enumerable: sharedBehaviour.propertyIsEnumerable(property) }); Object.defineProperty(_mixin, Symbol.hasInstance, { value: (i) => !!i[typeTag] }); return _mixin; }
const BookCollector = mixin({ addToCollection (name) { this.collection().push(name); return this; }, collection () { return this._collected_books || (this._collected_books = []); } }); class Person { constructor (first, last) { this.rename(first, last); } fullName () { return this.firstName + " " + this.lastName; } rename (first, last) { this.firstName = first; this.lastName = last; return this; } }; BookCollector(Person.prototype); const president = new Person(‘Barak‘, ‘Obama‘) president .addToCollection("JavaScript Allongé") .addToCollection("Kestrels, Quirky Birds, and Hopeless Egocentricity"); president.collection() //=> ["JavaScript Allongé","Kestrels, Quirky Birds, and Hopeless Egocentricity"]
function mixin (behaviour, sharedBehaviour = {}) { const instanceKeys = Reflect.ownKeys(behaviour); const sharedKeys = Reflect.ownKeys(sharedBehaviour); const typeTag = Symbol(‘isa‘); function _mixin (clazz) { for (let property of instanceKeys) Object.defineProperty(clazz.prototype, property, { value: behaviour[property], writable: true }); Object.defineProperty(clazz.prototype, typeTag, { value: true }); return clazz; } for (let property of sharedKeys) Object.defineProperty(_mixin, property, { value: sharedBehaviour[property], enumerable: sharedBehaviour.propertyIsEnumerable(property) }); Object.defineProperty(_mixin, Symbol.hasInstance, { value: (i) => !!i[typeTag] }); return _mixin; }
这一版的 _mixin 函数将实例行为糅合进类的原型,虽然没有支持任何对象那般的灵活,但是使用起来更方便:
const BookCollector = mixin({ addToCollection (name) { this.collection().push(name); return this; }, collection () { return this._collected_books || (this._collected_books = []); } }); class Person { constructor (first, last) { this.rename(first, last); } fullName () { return this.firstName + " " + this.lastName; } rename (first, last) { this.firstName = first; this.lastName = last; return this; } }; BookCollector(Person); const president = new Person(‘Barak‘, ‘Obama‘) president .addToCollection("JavaScript Allongé") .addToCollection("Kestrels, Quirky Birds, and Hopeless Egocentricity"); president.collection() //=> ["JavaScript Allongé","Kestrels, Quirky Birds, and Hopeless Egocentricity"]
至此,非常不错,但总觉得有点马后炮的感觉。我们可以利用一下“类即表达式”这一事实:
const BookCollector = mixin({ addToCollection (name) { this.collection().push(name); return this; }, collection () { return this._collected_books || (this._collected_books = []); } }); const Person = BookCollector(class { constructor (first, last) { this.rename(first, last); } fullName () { return this.firstName + " " + this.lastName; } rename (first, last) { this.firstName = first; this.lastName = last; return this; } });
const Author = mixin({ writeBook (name) { this.books().push(name); return this; }, books () { return this._books_written || (this._books_written = []); } }); const Person = Author(BookCollector(class { // ... }));
function annotation(target) { // Add a property on target target.annotated = true; } @annotation class MyClass { // ... } MyClass.annotated //=> true
const BookCollector = mixin({ addToCollection (name) { this.collection().push(name); return this; }, collection () { return this._collected_books || (this._collected_books = []); } }); @BookCollector class Person { constructor (first, last) { this.rename(first, last); } fullName () { return this.firstName + " " + this.lastName; } rename (first, last) { this.firstName = first; this.lastName = last; return this; } }; const president = new Person(‘Barak‘, ‘Obama‘) president .addToCollection("JavaScript Allongé") .addToCollection("Kestrels, Quirky Birds, and Hopeless Egocentricity"); president.collection() //=> ["JavaScript Allongé","Kestrels, Quirky Birds, and Hopeless Egocentricity"]
const BookCollector = mixin({ addToCollection (name) { this.collection().push(name); return this; }, collection () { return this._collected_books || (this._collected_books = []); } }); const Author = mixin({ writeBook (name) { this.books().push(name); return this; }, books () { return this._books_written || (this._books_written = []); } }); @BookCollector @Author class Person { constructor (first, last) { this.rename(first, last); } fullName () { return this.firstName + " " + this.lastName; } rename (first, last) { this.firstName = first; this.lastName = last; return this; } };
如果你想使用装饰器模拟“纯函数式复合”,这也是简单易行的惯常模式:
class Person { constructor (first, last) { this.rename(first, last); } fullName () { return this.firstName + " " + this.lastName; } rename (first, last) { this.firstName = first; this.lastName = last; return this; } }; @BookCollector @Author class BookLover extends Person {};
标签:为我 res 过程 返回 符号 技术 fir 包括 这不
原文地址:https://www.cnblogs.com/averyby/p/10027793.html