Javascript stringify '%%' 丢失百分号

Javascript stringify '%%' loses a percent sign

为什么 stringify 的输出缺少百分号?

var a = ["dp",'%%'];
var t = JSON.stringify (a);
console.log ('t: ' + t);

结果是:

t: ["dp","%"]

为什么不是结果:

t: ["dp","%%"]

谢谢!

console.log in Node.js, the function takes arguments in a printf-like way 的文档中所述:

The first argument is a string that contains zero or more placeholders.
Each placeholder is replaced with the converted value from its corresponding argument. Supported placeholders are:

%s - String.
%d - Number (both integer and float).
%j - JSON. Replaced with the string '[Circular]' if the argument contains circular references.
%% - single percent sign ('%'). This does not consume an argument.
If the placeholder does not have a corresponding argument, the placeholder is not replaced.

因此,在 console.log in Node.js(不是浏览器)打印的字符串中出现的任何 %% 都将被替换为单身%。任何 %s%d%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'.

但是,在浏览器中调用 console.log 只会打印带有上述替换 none 的纯字符串。

JSON.stringify没有任何关系。我可以通过 console.log('%%')node 0.10.36 重现单个 % 输出。这可能是节点内部用于 console.logutil.format 的问题。

https://nodejs.org/docs/latest-v0.10.x/api/util.html#util_util_format_format

然而,在 node 4.0 中,我得到了预期的结果。