是否可以原型(prototype)化 File 对象?我目前遇到的问题是,Firefox 7+ 中的 File 对象不再具有 fileName
属性,而是具有 name
属性。
我的代码广泛依赖于fileName
,我想找到一种比更改所有出现的情况更简单的方法。
最佳答案
试试这个:
(function(File){
if( !File ) {
return;
}
function getFileName(){
return this.name;
}
File.prototype.__defineGetter__( "fileName", getFileName );
})(window.File)
它应该对具有 fileName
的浏览器没有影响,因为只有在 File
实例对象本身中找不到原型(prototype)时才会查看原型(prototype)。
jsfiddle:http://jsfiddle.net/Sj3B2/2/ (在 Firefox、Chrome 和 Opera 中测试)
已编辑,因为 Opera 不支持 Object.defineProperty
关于javascript - 原型(prototype)化文件对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8203650/