Javascript: %s 或 %d 代表字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42406146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:07:36  来源:igfitidea点击:

Javascript: %s or %d represents string?

javascriptecmascript-6passport.js

提问by Turnipdabeets

Can someone please explain what's happening here? I see %dand %sbut I don't see these declared or written anywhere else in the code. What the heck does this mean/ do in javascript? I'm assuming it's a sort of string templating that I've never seen before?

有人可以解释一下这里发生了什么吗?我看到了%d%s但我没有看到这些声明或写在代码的其他地方。这到底是什么意思/在 javascript 中做什么?我假设这是一种我以前从未见过的字符串模板?

passport.deserializeUser(
  (id, done) => {
    debug('will deserialize user.id=%d', id)
    User.findById(id)
      .then(user => {
        debug('deserialize did ok user.id=%d', user.id)
        done(null, user)
      })
      .catch(err => {
        debug('deserialize did fail err=%s', err)
        done(err)
      })
  }
)

回答by Chris Cruz

What you are seeing there is the string substitution patterns that are built into console.log()or console.debug().

您在那里看到的是内置于console.log()or 中的字符串替换模式console.debug()

The pattern goes as I have presented below:

该模式如下所示:

%sfor a String

%s对于字符串

%dor%ifor Number

%d%i号码

%ffor Floating points

%f对于浮点

%ofor an Object

%o对于一个对象

%jfor an JSON

%j对于 JSON

So essentially you are replacing the specifier with the values supplied as so:

所以基本上你是用提供的值替换说明符,如下所示:

var name = 'Chris';
console.log('Hi, my name is %s.', name);
// Hi, my name is Chris.

console.debug('Hi, my name is %s.', name);
// Hi, my name is Chris.

Docs:

文档:

回答by Teocci

console.log()and console.debug()use printf-style formatting. Below are the officially supported formatters:

console.log()console.debug()使用 printf 样式的格式。以下是官方支持的格式化程序:

Formatter representation:

格式化程序表示:

  • %OPretty-print an Object on multiple lines.
  • %oPretty-print an Object all on a single line.
  • %sString.
  • %dNumber (both integer and float).
  • %jJSON. Replaced with the string '[Circular]' if the argument contains circular references.
  • %%Single percent sign ('%'). This does not consume an argument.
  • %O在多行上漂亮地打印一个对象。
  • %o在一行上漂亮地打印一个对象。
  • %s细绳。
  • %d数字(整数和浮点数)。
  • %jJSON。如果参数包含循环引用,则替换为字符串 '[Circular]'。
  • %%单个百分号 ('%')。这不消耗参数。

The results are written in the debug console. just open your command-line or terminal and run it using this:

结果写在调试控制台中。只需打开您的命令行或终端并使用以下命令运行它:

node debug [script.js | -e "script" | <host>:<port>] command

回答by Genovo

The % output Format specifiers %d %s etc come from ANSI C so it is coming into node thru a library -- in this case util.format https://nodejs.org/api/util.html#util_util_format_format_args

% 输出格式说明符 %d %s 等来自 ANSI C,因此它通过库进入节点 - 在这种情况下 util.format https://nodejs.org/api/util.html#util_util_format_format_args

回答by Bruno Santos

That is probably something specific of the debug(), since there is no built-in string formatting in Javascript (not without a library).

这可能是 debug() 的特定内容,因为 Javascript 中没有内置的字符串格式(不是没有库)。

However, %d is replaced by an integer and the %s is replaced by a string. Example:

但是,%d 被整数替换,%s 被字符串替换。例子:

debug("I'm %s and I'm %d years old", "John", 10)

Should print: I'm John and I'm 10 years old.

应该打印:我是约翰,我 10 岁。

Here is a library you could use if you're interested: http://www.diveintojavascript.com/projects/javascript-sprintf

如果您有兴趣,可以使用以下库:http: //www.diveintojavascript.com/projects/javascript-sprintf

回答by Mucahit ASLANCAN

You can give a style to log text with CSS.

您可以提供一种样式来使用 CSS 记录文本。

console.log("%c YourText", "color:blue; font-weight:bold;");

You can create multiple style for different console log text.

您可以为不同的控制台日志文本创建多种样式。

console.log("%c Text1 %c Text2 %c Text3", "color:red;", "color:green;", "color:blue;");

回答by aGuegu

for simple format like %03d, lodash.padStartcould just do the job.

对于简单的格式,如%03dlodash.padStart可以完成这项工作。

_.padStart(x, 3, '0')