Javascript 大写字符串类型
我有一些类似于下面的代码用于下载二进制文件。我的目标是将其转换为base64数据URI,同时支持可能不了解ArrayBuffer的旧浏览器。现在,代码似乎工作得很好Javascript 大写字符串类型,javascript,google-closure-compiler,Javascript,Google Closure Compiler,我有一些类似于下面的代码用于下载二进制文件。我的目标是将其转换为base64数据URI,同时支持可能不了解ArrayBuffer的旧浏览器。现在,代码似乎工作得很好 function download (url) { 'use strict'; var xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.responseType = 'arraybuffer'; xhr.onreadyst
function download (url) {
'use strict';
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
var mime = xhr.getResponseHeader('Content-Type');
var base64;
if (oldIE) {
var rawBytes = ieConvert(xhr.responseBody);
base64 = encodeString64(rawBytes);
} else if (xhr.response instanceof ArrayBuffer) {
var payload = new Uint8Array(xhr.response);
for (var i = 0, buffer = ''; i < payload.length; i++) {
buffer += String.fromCharCode(payload[i]);
}
base64 = window.btoa(buffer);
} else if (xhr.response instanceof String) {
base64 = encodeString64(xhr.response);
}
return 'data:' + mime + ';base64,' + base64;
} else if (xhr.readyState === 4) {
throw "Failed.";
}
};
xhr.send();
}
你知道如何摆脱这个警告吗
base64 = encodeString64(String(xhr.response));
尝试以下方法以了解差异:
<script>
x = new String("hello");
alert(typeof x); //prints Object (this is a String object)
x = String("hello"); //or x = "hello";
alert(typeof x); //prints string (this is string with small s - similar to running toString() on a JS object)
</script>
x=新字符串(“你好”);
警报(x型)//打印对象(这是一个字符串对象)
x=字符串(“你好”)//或x=“你好”;
警报(x型)//打印字符串(这是带有小s的字符串-类似于在JS对象上运行toString())
哪里定义了encodeString64()?你能发布那段代码吗?它真的没什么特别的,而且无论出于什么目的,它的功能都与字符串相同。string
类型可以在任何需要string
的地方使用,但不能反过来使用。这就是你得到警告的原因。您还可以使用xhr.response.toString()
。
<script>
x = new String("hello");
alert(typeof x); //prints Object (this is a String object)
x = String("hello"); //or x = "hello";
alert(typeof x); //prints string (this is string with small s - similar to running toString() on a JS object)
</script>