简体   繁体   English

console.log中的和和+有什么区别?

[英]What is the difference between , and + in the console.log?

pt=new Date(2019,11,12,8,2,3)

console.log(pt.getFullYear()," ",pt.getMonth());

gives result 2019 " " 11 给出结果2019 " " 11

console.log(pt.getFullYear()+" "+pt.getMonth());

gives the result as 2019 11 给出结果为2019 11

What is the difference between using, and + in this example? 在此示例中,使用和+有什么区别?

其中第一个将三个单独的参数提供给console.log,第二个将三个参数附加在一起,然后将其作为单个参数传递给console.log。

With the (,) you're with the console.log you're requesting to show a separate group of items as string, making a kind of array. 使用(,)时,您需要使用console.log,要求将一组单独的项目显示为字符串,从而形成一种数组。 When you put the (+) symbol you are adding the strings, and in this case the " " is just adding a space between the first and the second string. 当您放置(+)符号时,您正在添加字符串,在这种情况下,“”只是在第一和第二个字符串之间添加一个空格。 It is called concatenation. 这称为串联。

console.log is part of the Console API and is accesible in various browsers. console.log是控制台API的一部分,可在各种浏览器中访问。 You can find its full documentation on MDN . 您可以在MDN上找到其完整文档。

It states that console log has the following parameters: 它指出控制台日志具有以下参数:

obj1 ... objN

A list of JavaScript objects to output. 要输出的JavaScript对象列表。 The string representations of each of these objects are appended together in the order listed and output. 这些对象的每个字符串表示形式都按列出的顺序附加在一起并输出。

So, when you concatenate the parameters you pass only one object to the function and when you pass multiple parameters console.log will do the concatenation for you. 因此,当串联参数时,仅将一个对象传递给函数,而当传递多个参数时, console.log将为您完成串联。

console.log(pt.getFullYear()," ",pt.getMonth());

The above example passes three separate arguments to console.log. 上面的示例将三个单独的参数传递给console.log。 What it outputs depends on how console.log is implemented. 它输出的内容取决于console.log的实现方式。 It has changed over time and is little bit different between browsers. 随着时间的推移,它已经发生了变化,并且在不同的浏览器之间也没有什么不同。 When invoked with arguments like in the example, it has access to the variables and can display them with some magic depending on type, for example if they are arrays or objects. 当像示例中那样使用参数调用时,它可以访问变量,并且可以根据类型(例如,它们是数组还是对象)用某种魔术来显示它们。 In your example it is displayed as: 在您的示例中,它显示为:

2019 " " 11

where the numbers are in blue text, indicating that it was a variable of type number, and the empty string is shown in red, indicating that is was a string. 其中的数字为蓝色文本,表示它是数字类型的变量,空字符串显示为红色,表示这是字符串。

Compare this to the following example, where it all is converted to a string before being passed to console.log in one argument: 与此相比,下面的例子中,在那里它所有被传递到前转换为字符串console.log在一个参数:

console.log(pt.getFullYear()+" "+pt.getMonth());

where it is displayed as 显示为

2017 5

with black text, indicating that it was passed as a string in the first parameter. 黑色文本,表示已在第一个参数中将其作为字符串传递。

The first parameter to console.log can be used as a format string, like printf in c and other languages. console.log的第一个参数可以用作格式字符串,例如c和其他语言的printf For example 例如

console.log( "%d %d", pt.getFullYear(), pt.getMonth() );

where %d is a place holder for a number. 其中%d是数字的占位符。 The output is in black text and gives the exact same output as your second example. 输出为黑色文本,并提供与第二个示例完全相同的输出。

console.log("%d %d", pt.getFullYear(),pt.getMonth(), pt.getDate());

In the example above, the year and month will be shown in black text, but the date will be in blue. 在上面的示例中,年和月将以黑色文本显示,而日期将以蓝色显示。 This is because the format string only have two placeholders, but there are three arguments. 这是因为格式字符串只有两个占位符,但是有三个参数。 console.log show the extra arguments, using the magic. console.log使用魔术显示额外的参数。

Documentation: 说明文件:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM