我正在尝试通过 RequireJs 在 js 文件请求中添加水印:
requirejs.config({
baseUrl: '/app',
urlArgs: [98, 121, 65, 68, 77].map(String.fromCharCode).join('')+'&v=1.0.0'
});
由于某些原因我不能直接输入byADM
,所以我写了上面的代码。
我的问题是加入字符后它显示 b%00%00y%01%00A%02%00D%03%00M%04%00&v=1.0.0
而不是 byADM&v=1.0.0
在网址中。它看起来像 ''
转换为 %00
。
我该怎么做才能驾驭它?
字符串库或其他内容中是否有任何真正的空字符?
最佳答案
Array.prototype.map
将三个参数 传递给回调:currentValue
、index
、数组
。 String.fromCharCode
接受任意数量的参数,一次性将它们全部转换。本质上,您是通过将 String.fromCharCode
作为回调直接传递给 map
来调用 String.fromCharCode(98, 0, [])
。
您需要剔除额外的参数:
[98, 121, 65, 68, 77].map((c) => String.fromCharCode(c))
但是,除了对抗这种行为,您还可以使用 String.fromCharCode
接受任意数量参数的能力来简化您的代码:
String.fromCharCode.apply(String, [98, 121, 65, 68, 77]) + '&v=1.0.0'
或者,你知道……
String.fromCharCode(98, 121, 65, 68, 77) + '&v=1.0.0'
关于JavaScript Array join() 导致结果中出现空 (%00) 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38715191/