HTML5之FileWriter继承扩展,增加方法或者对象
来源:互联网 发布:C语言输入字符串 编辑:程序博客网 时间:2024/06/16 11:05
因为我们知道JavaScript里面如果要实现对象的方法重写或者新增方法都是首先要获取对象本身,然后根据这个对象来获取属性和方法或者增加方法,如下就是如何实现FileWriter的新增方法。
function FileWriter(writer){ this.writer = writer; // EventTarget this.addEventListener = function(type, listener, useCapture) { this.writer.addEventListener(type, listener, useCapture); }; this.removeEventListener = function(type, listener, useCapture) { this.writer.removeEventListener(type, listener, useCapture); }; this.dispatchEvent = function(evt) { this.writer.dispatchEvent(evt); }; // FileSaver const INIT = this.writer.INIT; const WRITING = this.writer.WRITING; const DONE = this.writer.DONE; this.__defineGetter__("readyState", function(){return this.writer.readyState;}); this.__defineGetter__("error", function(){return this.writer.error;}); this.__defineGetter__("fileName", function() {return this.writer.fileName;}); this.abort = function() { this.writer.abort(); }; this.__defineGetter__("onwritestart", function(){return this.writer.onwritestart;}); this.__defineGetter__("onprogress", function(){return this.writer.onprogress;}); this.__defineGetter__("onabort", function(){return this.writer.onabort;}); this.__defineGetter__("onwrite", function(){return this.writer.onwrite;}); this.__defineGetter__("onerror", function(){return this.writer.onerror;}); this.__defineGetter__("onwriteend", function(){return this.writer.onwriteend;}); this.__defineSetter__("onwritestart", function(val){this.writer.onwritestart=val;}); this.__defineSetter__("onprogress", function(val){this.writer.onprogress=val;}); this.__defineSetter__("onabort", function(val){this.writer.onabort=val;}); this.__defineSetter__("onwrite", function(val){this.writer.onwrite=val;}); this.__defineSetter__("onerror", function(val){this.writer.onerror=val;}); this.__defineSetter__("onwriteend", function(val){this.writer.onwriteend=val;}); // FileWriter this.__defineGetter__("position", function(){return this.writer.position;}); this.__defineGetter__("length", function(){return this.writer.length;}); this.write = function(data) { this.writer.write(new Blob([data], {type : "text/plain;charset=UTF-8"})); }; this.seek = function(offset) { this.writer.seek(offset); }; this.truncate = function(size) { this.writer.truncate(size); };}