美文网首页
console.log 格式化输出

console.log 格式化输出

作者: GC风暴 | 来源:发表于2019-10-15 11:57 被阅读0次

写在前面

在开发调试前端工程时,经常会遇到需要格式化输出的需求, 那么该如何操作呢?

发车咯...

1.先看5类基本操作

1.console.log用于输出普通信息

2.console.info用于输出提示性信息

3.console.error用于输出错误信息

4.console.warn用于输出警示信息

5.console.debug用于输出调试信息

5类基本输出验证

console对象的上面5种方法,都可以使用 printf风格的占位符。
不过,占位符的种类比较少,只支持

  • 字符%s)、
image.png
  • 整数%d%i)、

    image.png
  • 浮点数%f)、

    image.png
  • 对象%o)四种:

    image.png

ES6模板输出

ES6模板的使用反单引号``,支持变量(使用${ params } 格式), 支持换行, 支持占位符,

let dog = {color: "土黄色", type:"中华田园犬", age: 1}
console.log(`
Dog info:  
  color: ${dog.color}
  type:  ${dog.type}
  color: %d
`, age)

相关文章

网友评论

      本文标题:console.log 格式化输出

      本文链接:https://www.haomeiwen.com/subject/avujmctx.html