我有这个功能:
function ungarble(garble){
var s = "";
for( var i = 0; i < garble.length; i++ ) {
s += String.fromCharCode(garble[i]);
}
return s;
}
它接收一个charCodes数组,然后返回那些charCodes表示的字符串。
本机Javascript是否具有执行此功能的功能?
注意:这是为了阅读
child_process.spawn
返回的消息。 最佳答案
fromCharCode
已经接受任何数量的参数以转换为字符串,因此您可以简单地使用apply
为它提供一个数组:
var chars = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100];
var str = String.fromCharCode.apply(null, chars);
console.log(str);
或使用ES6 spread operator
var chars = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100];
var str = String.fromCharCode(...chars);
console.log(str);