本章介紹一下命令的基類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;
};