我是一名初级开发者,正在调试文件上传器。我有这行代码,我无法理解:
var ext = file.name.slice((file.name.lastIndexOf('.') - 1 >>> 0) + 2);
有人能用简单的英语翻译这一行吗?
谢谢!
答案 0 :(得分:0)
在一行中,该代码试图通过查看最后一个点之后的扩展名来获取文件类型。
尝试以下代码段,您可以通过拆分行轻松调试javascript。
var filename = "hellofile.12345.txt"; // = file.name;
var indexOfDotB4Exten = filename.lastIndexOf('.');
console.log('last Dot: ' + indexOfDotB4Exten);
// Next step is more crazy
// Ideally it should get Index of file extenion, i.e. next position after last dot (.)
// Is almost equal to indexOfDotB4Exten + 1
// But (1 >>> 0) will make sure your index is within range
var sliceFrom = (indexOfDotB4Exten - 1 >>> 0 ) + 2;
var ext = filename.slice(sliceFrom);
console.log("sliceFrom: " + sliceFrom + " ext:" +ext);