gpt4 book ai didi

javascript - 大写字符串类型

转载 作者:行者123 更新时间:2023-11-29 22:20:38 26 4
gpt4 key购买 nike

我有一些类似于下面的代码来下载一个二进制文件。我的目标是将其转换为 base64 数据 URI,同时支持可能不了解 ArrayBuffer 的旧浏览器。现在,代码似乎运行良好。

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();
}

我的问题是,当我使用 Google Closure Compiler 时,我收到类型警告。很明显,是因为我用了instanceof String,但是instanceof string不行,因为对象名要大写。

WARNING - actual parameter 1 of encodeString64 does not match formal parameter
found : String
required: string
base64 = encodeString64(xhr.response);
^
0 error(s), 1 warning(s), 85.22012578616352 typed

知道如何摆脱这个警告吗?

最佳答案

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>

关于javascript - 大写字符串类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12606485/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com