本章介绍一下命令的基类Command.js,这个文件是编辑器的所有操作命令共性提取的文件。
//命令基类(编辑的对象)
var Command = function ( editorRef ) {
this.id = - 1; //命令的id,主要是对应history中的序号
this.inMemory = false; //命令可以在内存中,也可以序列化到json中,方便存储
this.updatable = false; //命令是否可以更新??
this.type = ''; //命令的类型
this.name = ''; //命令的名称
if ( editorRef !== undefined ) {
//对象级别的editor引用,每个新创建的对象都有这个成员
Command.editor = editorRef; //使用函数调用方式传入的editor,而不是new方式,静态成员
}
this.editor = Command.editor; //命令编辑
};
//原型上添加函数
Command.prototype.toJSON = function () {
//返回对象
var output = {};
output.type = this.type;
output.id = this.id;
output.name = this.name;
return output;
};
//原型上添加函数
Command.prototype.fromJSON = function ( json ) {
//设置成员
this.inMemory = true;
this.type = json.type;
this.id = json.id;
this.name = json.name;
};