简体   繁体   English

在javavascript提示(),document.write()和console.log()中使用“ +”(加号)和“,”(逗号)

[英]Using the “+” (plus) and the “,” (comma) in javavascript prompt(), document.write() and console.log()

I am a bit confused as to how JavaScript is interpreted for the following: 对于以下内容如何解释JavaScript,我有些困惑:

1. 1。

n = prompt("Please enter a number to calculate factorial " + "Now!", "Delete this first!" + " ...And this :(", "Hello");

I assume that the comma "," is used to separate the arguments sent to the function, and that prompt() takes either one(1) or two(2) arguments and discards the third one: "Hello" ? 我假设用逗号“,”分隔发送给该函数的参数,并且hint()接受one(1)或two(2)参数并丢弃第三个参数:“ Hello”吗?

2. 2。

How are "," commas interpreted in the document.write() function and the console.log() function. 

Are they used to separate arguments? 他们习惯于分开论点吗? Can these functions take an infinite number of arguments? 这些函数可以接受无数个参数吗?

3. 3。

In these examples: 在这些示例中:

console.log("1.) expression " , (1<2).toString() + 3>2);

console.log("2.) expression " , (3<2).toString() , 3>2);

console.log("3.) expression " + (3>2).toString() + 3>2);

console.log("4.) expression " , 3<2);

console.log("5.) expression " + 3<2);

console.log("6.) expression " + n + 3<2);

OUTPUTS: 输出:

1.) expression  false

2.) expression  false true

3.) false

4.) expression  false

5.) false

6.) false

I'm guessing the "+" is treated as part of the expression and not to concatenate strings when it encounters another operator? 我猜“ +”被视为表达式的一部分,并且在遇到另一个运算符时不将字符串连接起来?

In the first expression, was (1<2).toString() added to 3 ? 在第一个表达式中,是否将(1<2).toString()添加到3 What kind of type and what value was it converted to? 它转换为哪种类型和什么值? Why does it evaluate to false ? 为什么将其评估为false

Can I assume that the document.write() and console.log() functions treat the "," (comma) differently than other functions? 我是否可以假设document.write()console.log()函数与其他函数对待“,”(逗号)不同?

  1. Regarding prompt 关于提示

I assume that the comma "," is used to separate the arguments sent to the function, and that prompt() takes either one(1) or two(2) arguments and discards the third one: "Hello" ? 我假设用逗号“,”分隔发送给该函数的参数,并且hint()接受one(1)或two(2)参数并丢弃第三个参数:“ Hello”吗?

prompt takes two arguments. 提示有两个参数。 Any extras are ignored as is the case of all functions in JavaScript that don't use the arguments object or rest parameters to change behaviour for a variable number of arguments. 就像JavaScript中所有不使用arguments对象或rest参数来更改可变数量参数的行为的函数一样,忽略所有其他功能。

function x ( a ) { return a; }
x( 'this', 'is', 'perfectly' ,'valid' );
  1. Regarding document.write and console.log 关于document.write和console.log

Are they used to separate arguments? 他们习惯于分开论点吗? Can these functions take an infinite number of arguments? 这些函数可以接受无数个参数吗?

Yes, a comma in a function call is always used to separate arguments. 是的,函数调用中的逗号始终用于分隔参数。 As far as I know both functions make use of a variable amount of arguments, but it is likely implementation specific. 据我所知,这两个函数都使用可变数量的参数,但这可能是特定于实现的。

  1. I'll add some parenthesis to help you understand the output from those lines: 我将添加一些括号以帮助您了解这些行的输出:

console.log("1.) expression " , ( (1<2).toString() + 3 ) > 2 );
console.log("2.) expression " , (3<2).toString() , 3>2);
console.log( ( "3.) expression " + (3>2).toString() + 3 ) > 2);
console.log("4.) expression " , 3<2);
console.log( ( "5.) expression " + 3 ) <2);
console.log( ( "6.) expression " + n + 3 ) <2);

Can I assume that the document.write() and console.log() functions treat the "," (comma) differently than other functions? 我是否可以假设document.write()和console.log()函数与其他函数对待“,”(逗号)不同?

You would be wrong to assume that, but I won't try and stop you. 假设您这样做是错误的,但我不会尝试阻止您。

Disclaimer I am writing this from my understanding of Javascript and how it works. 免责声明我从的Javascript的理解,以及如何它的工作原理写这个。 I was never formally taught Javascript, I did not study this language in college, and I've never looked this up. 我从来没有正式教过Javascript,也没有在大学里学习过这种语言,也从没有看过。 If it is wrong, please understand that this is my(working) knowledge of how Javascript works. 如果错了,请理解这是对Javascript如何工作的(有效)知识

The Functions 功能

There's quite a bit to talk about for your question. 您的问题有很多要谈论的。 First and foremost is that each of those functions are different in what they require or will accept. 首先,最重要的是,这些功能在要求或接受方面有所不同。 Prompt() accepts only two arguments: the text it outputs and the "Default text" (the text that shows up in the prompt originally). Prompt()仅接受两个参数:输出的文本和“默认文本”(最初显示在提示中的文本)。 You can put in more arguments, but the function isn't coded to do anything with them so nothing happens. 您可以添加更多参数,但是该函数未进行编码以对它们执行任何操作,因此没有任何反应。

The console.log() function, much like the document.write() function take a variable (and theoretically infinite) number of arguments. console.log()函数与document.write()函数非常类似,它接受可变数量(理论上是无限个)的参数。 Each argument is evaluated and passed to those functions. 每个参数都会被求值并传递给那些函数。 Console.log and document.write will both simply output those expressions to their respective locations (the Javascript Console or the document object) Console.logdocument.write都将简单地将这些表达式输出到它们各自的位置(Javascript控制台或文档对象)

The + Operator +运算符

This is the trickier part to answer, and is again written from entirely my own understanding of Javascript. 这是要回答的棘手部分,也是完全根据我自己对Javascript的理解编写的。 The + operator in Javascript is dual function. Javascript中的+运算符是双重功能。 It works as both the mathematical operation of addition and the non-integer concactenator. 它可以作为另外的两个数学运算非整数concactenator。

What I mean by that long string of words is this: Javascript, when it encounters an expression with a + in it, does its best to guess as to what is expected of it. 我所说的那长串字是什么意思:当Javascript遇到其中带有+的表达式时,它会尽最大的努力猜测它的期望值。 If both items are numbers (integers, floats, doubles, boolean) it decides that you want to add the two things together. 如果两个项目都是数字(整数,浮点数,双精度数,布尔值),则它决定您要将两个内容加在一起。 If either one is not a number, then it decides you want to concat the two things together. 如果其中一个不是数字,则它决定您要同时合并这两个东西。 This isn't always correct, and sometimes it baffles even the best of us. 这并不总是正确的,有时甚至困扰着我们最好的人。

Your console tests look a little like this in the world of behind-the-scenes javascript. 在幕后javascript的世界中,您的控制台测试看起来像这样。

console.log("1.) expression ", (1 < 2).toString() + 3 > 2);
//gets evaluated to :
//"1.) expression ", "true"+3 > 2)
//= "1.) expression", "true3" > 2) <- can't compare these two logically, so we get false
//= "1.) expression", false <- is finally passed to function
console.log("2.) expression ", (3 < 2).toString(), 3 > 2);
//"2.) expression ", "false" , true
console.log("3.) expression " + (3 > 2).toString() + 3 > 2);
//"3.) expression " + "false" + 3 > 2
//="3.) expression false" + 3 > 2
//="3.) expression false3" > 2 <- same thing, can't logically compare, so false.
console.log("4.) expression ", 3 < 2);
//"4.) expression ", false
console.log("5.) expression " + 3 < 2);
//"5.) expression 3" < 2 <- can't logically compare
//false
console.log("6.) expression " + n + 3 < 2);
//Same thing as #4, but not with an extra variable that gets combined to the string.

I think of this like order of operations, prioritizing grouped expressions first and then going left to right. 我认为这就像操作顺序,先对分组表达式进行优先排序,然后再从左到右进行排序。

Hope this helps. 希望这可以帮助。

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

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