库的形状被描述为in the docs.它有:
type JetStream : {
model: (name: String, fn: ModelDefiner) => Model,
因此回调是一个ModelDefiner,定义如下:
type ModelDefiner : (this: Model) => void
所以this
是Model
,其定义如下:
type ModelObject : {
has: (propertyName: String, PropertyType) => void,
setScope: (scope: Scope, Callback<Error, void>) => void,
getValues: () => Object,
getAddSyncFragment: () => SyncFragment<"add">
}
-- A Model instance inherits from ModelObject.
type Model : ModelObject & {
typeName: String,
uuid: ObjectUUID,
scope: Scope | null
}
正如您在上面看到的,Model扩展了ModelObject,而ModelObject有一个has
方法.
如果您想知道如何编写自己可以做到这一点的代码,那么您真正需要做的就是创建一个具有has
方法的对象,并使用它创建.call
个回调函数.
class Model {
has(str, type) {
console.log('has called with', str, type);
};
}
const createModel = (str, callback) => {
const model = new Model();
callback.call(model);
};
var Shape = createModel('Shape', function() {
this.has('x', Number);
this.has('y', Number);
this.has('width', Number);
this.has('height', Number);
this.has('type', Number);
});