为什么 stringify 的输出缺少百分号?
var a = ["dp",'%%'];
var t = JSON.stringify (a);
console.log ('t: ' + t);
结果是:
t: ["dp","%"]
为什么没有结果:
t: ["dp","%%"]
Thanks!
正如文档中所指定的console.log在 Node.js 中,该函数采用 a 中的参数类似 printf 的方式:
第一个参数是包含零个或多个占位符的字符串。
每个占位符都替换为其相应参数的转换值。支持的占位符有:
%s - 字符串。
%d - 数字(整数和浮点数)。
%j - JSON。如果参数包含循环引用,则替换为字符串“[Circular]”。
%% - 单个百分号 ('%')。这不会消耗参数。
如果占位符没有相应的参数,则不会替换占位符。
因此,任何发生%%
在打印的字符串中console.log
在 Node.js 中(不是浏览器)将被替换为一个%
. Any %s
, %d
or %j
将分别替换为字符串、数字或 JSON 字符串。这里有些例子:
console.log("This is a string: %s", "Hello, World!");
//= This is a string: Hello, World!
console.log("This is a number: %d", 3.14);
//= This is a number: 3.14
console.log("This is a JSON string: %j", { value: true });
//= This is a JSON string: {"value":true}
console.log("This is a single percentage sign: %%");
//= This is a single percentage sign: %
console.log("If we use %%s and %%d here we get '%s' and '%d'.", "a string", 100);
//= If we use %s and %d here we get 'a string' and '100'.
Calling console.log
然而,在浏览器中,只会打印纯字符串,而不进行上述任何替换。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)