我正在使用 Immutable.js,并且我想扩展 Immutable.Record,因此我有一个工作原理相同但添加了一些默认属性和函数的类。
查看源码,Record.js 是一个 ES6 类,它扩展了 KeyedCollection,但从构造函数返回一个函数。
https://github.com/facebook/immutable-js/blob/master/src/Record.js
我不太明白这样做的副作用。
基本上我想要一个 FooRecord 类,我可以在其中执行此操作:
class FooifiedRecord extends FooRecord({
name: '',
}){
sayMyName(){
return this.name;
}
};
它相当于:
class FooifiedRecord extends Immutable.Record({
name: '',
foo: '',
}) {
doFoo(){
return this.foo + ' bar';
}
sayMyName(){
return this.name;
}
}
但我不确定语法是否正确。
编辑:与 How to construct subclasses of Immutable.Record? 不太相似,涵盖了 Immutable.Record 的基本使用。我想知道如何扩展它。
最佳答案
如果您不打算使用 new 实例化您的基类,您可以执行 justinko recommends here 操作。 :
import { Record } from 'immutable'
const Base = defaultValues => class extends Record({
id: undefined,
links: {},
errors: {},
...defaultValues
}) {
}
class User extends Base({
firstName: undefined,
lastName: undefined
}) {
fullName() {
return this.firstName + ' ' + this.lastName
}
}
关于javascript - 在 Immutable.js 中扩展记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42653048/