博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
为什么('b'+'a'+ +'a'+'a')。toLowerCase()'banana'的结果?
阅读量:2289 次
发布时间:2019-05-09

本文共 6575 字,大约阅读时间需要 21 分钟。

本文翻译自:

I was practicing some JavaScript when one of my friends came across this JavaScript code: 当我的一个朋友遇到以下JavaScript代码时,我正在练习一些JavaScript:

document.write(('b' + 'a' + + 'a' + 'a').toLowerCase());

The above code answers "banana" ! 上面的代码回答"banana" Can anyone explain why? 谁能解释为什么?


#1楼

参考:


#2楼

+'a' resolves to NaN ("Not a Number") because it coerces a string to a number, while the character a cannot be parsed as a number. +'a'解析为NaN (“非数字”),因为它将字符串强制为数字,而字符a不能解析为数字。

document.write(+'a');
To lowercase it becomes banana .
小写变成banana

Adding NaN to "ba" turns NaN into the string "NaN" due to type conversion, gives baNaN . NaN添加到"ba"会由于类型转换而将NaN变成字符串"NaN" ,得到baNaN And then there is an a behind, giving baNaNa . 然后是a后面是baNaNa

The space between + + is to make the first one string concatenation and the second one a unary plus (ie "positive") operator. + +之间的空格是使第一个字符串串联,而第二个字符串成为一元加号(即“正”)运算符。 You have the same result if you use 'ba'+(+'a')+'a' , resolved as 'ba'+NaN+'a' , which is equivalent to 'ba'+'NaN'+'a' due to type juggling. 如果使用'ba'+(+'a')+'a' ,则得到相同的结果,解析为'ba'+NaN+'a' ,等效于'ba'+'NaN'+'a'打杂耍。

document.write('ba'+(+'a')+'a');


#3楼

('b' + 'a' + + 'a' + 'a').toLowerCase()

For clarity, let's break this down into two steps. 为了清楚起见,我们将其分为两个步骤。 First, we get the value of the parenthesized expression and then we apply the toLowerCase() function on the result. 首先,我们获得括号表达式的值,然后在结果上应用toLowerCase()函数。

Step one 第一步

'b' + 'a' + + 'a' + 'a'

Going LR , we have: 走向LR ,我们有:

  • 'b' + 'a' returns ba , this is regular concatenation. 'b' + 'a'返回ba ,这是常规连接。
  • ba + + 'a' attempts to concatenate ba with + 'a' . ba + + 'a'尝试将ba+ 'a' However, since the unary operator + attempts to convert its operand into a number, the value NaN is returned, which is then converted into a string when concatenated with the original ba - thus resulting in baNaN . 但是,由于一元运算符+试图将其操作数转换为数字,所以将返回值NaN ,然后将其与原始ba串联时将其转换为字符串-从而得到baNaN
  • baNaN + 'a' returns baNaNa . baNaN +'a'返回baNaNa Again, this is regular concatenation. 同样,这是常规连接。

At this stage, the result from step one is baNaNa . 在这一阶段,第一步的结果是baNaNa

Step two 第二步

Applying .toLowerCase() on the value returned from step one gives: .toLowerCase()应用于从第一步返回的值将得出:

banana 香蕉

There are many in JavaScript that you can check out. 您可以签出JavaScript中许多 。


#4楼

'b' + 'a' + + 'a' + 'a'

...is evaluated as.... 被评估为...

('b') + ('a') + (+'a') + ('a')

(see: ) (请参阅: )

(+'a') attempts to convert 'a' to a number using the . (+'a')尝试使用将'a'转换为数字。 Because 'a' is not a number, the result is ( "Not-A-Number" ): 因为'a'不是数字,所以结果为 ( “ Not-A-Number” ):

'b'  +  'a'  +  NaN  + 'a'

Although NaN stands for "Not a Number", it's still a numeric type ; 尽管NaN代表“不是数字”,但它仍然是数字类型 when added to strings, it concatenates just as any other number would: 当添加到字符串中时,它的连接方式与任何其他数字相同:

'b'  +  'a'  +  NaN  + 'a'  =>  'baNaNa'

Finally, it's lowercased: 最后,将其小写:

'baNaNa'.toLowerCase()      =>  'banana'

#5楼

It's just because of + operator. 只是因为+运算符。

We can get further knowledge from chunk it. 我们可以从中获得更多知识。

=> ( ('b') + ('a') + (++) + ('a') + ('a'))=> ( ('b') + ('a') + (+) + ('a') + ('a')) // Here + + convert it to +operator Which later on try to convert next character to the number.

For example 例如

const string =  '10';

You can convert a string into number by 2 ways: 您可以通过两种方式将字符串转换为数字:

  1. Number(string); 数字(字符串);
  2. +string; +字串;

So back to the original query; 回到原始查询; Here it tries to convert the next char ('a') to the number but suddenly we got error NaN, 在这里,它尝试将下一个字符('a')转换为数字,但突然我们收到错误NaN,

( ('b') + ('a') + (+'a') + ('a'))( ('b') + ('a') + NaN + ('a'))

But it treats as a string because the previous character was in the string. 但是它被视为字符串,因为前一个字符在字符串中。 So it will be 所以会的

( ('b') + ('a') + 'NaN' + ('a'))

And last it converts it to toLowerCase(), So it would be banana 最后将其转换为LowerCase(),因此它是香蕉

If you are put number next to it, Your result will be change. 如果您旁边输入数字,您的结果将改变。

( 'b' + 'a' +  + '1' + 'a' )

It would be 'ba1a' 就是“ ba1a”

const example1 = ('b' + 'a' + + 'a' + 'a').toLowerCase(); // 'banana' const example2 = ('b' + 'a' + + '1' + 'a').toLowerCase(); // 'ba1a' console.log(example1); console.log(example2);


#6楼

This line of code evaluates an expression and then calls a method based on the returned value. 这行代码评估一个表达式,然后根据返回的值调用一个方法。

The expression ('b' + 'a' + + 'a' + 'a') is solely composed of string literals and addition operators. 表达式('b' + 'a' + + 'a' + 'a')仅由字符串文字和加法运算符组成。

  • "A string literal is zero or more characters enclosed in single or double quotes." “字符串文字是用单引号或双引号引起来的零个或多个字符。”
  • "The addition operator either performs string concatenation or numeric addition." “加法运算符执行字符串连接或数字加法。”

An implicit action taken is the call for ToNumber on a string 采取的隐式操作是在字符串上调用ToNumber

  • "ToNumber applied to Strings applies grammar to the input String. If the grammar cannot interpret the String as an expansion of StringNumericLiteral, then the result of ToNumber is NaN." ToNumber“应用于字符串的ToNumber将语法应用于输入的String。如果语法无法将String解释为StringNumericLiteral的扩展,则ToNumber的结果为NaN。”

The interpreter has rules of how to parse the expression, by breaking it down into its components of left and right hand expressions. 解释器具有将表达式分解为左右两个表达式的组成部分的规则。


Step 1: 'b' + 'a' 步骤1: 'b' + 'a'

Left Expression: 'b' 左表达: 'b'

Left Value: 'b' 左值:“ b”

Operator: + (one of the expression sides is a string, so string concatenation) 运算符:+(表达式的一面是字符串,因此是字符串连接)

Right Expression: 'a' Right Value: 'a' 正确的表达: 'a'正确的值:'a'

Result: 'ba' 结果: 'ba'


Step 2: 'ba' + + 'a' 步骤2: 'ba' + + 'a'

Left Expression: 'ba' 左表情: 'ba'

Left Value: 'ba' 左值:“ ba”

Operator: + (one of the expression sides is a string, so string concatenation) 运算符:+(表达式的一面是字符串,因此是字符串连接)

Right Expression: + 'a' (this evaluates the Math Value of the character 'a' assuming that it is a positive number from the + sign -- the minus sign would have also worked here indicating a negative number -- which results in NaN) 正确的表达式: + 'a' (这会假设字符'a'的数学值,假设它是+号的正数-减号在这里也可以表示负数,这会导致NaN )

Right Value: NaN (because the operator is string concatenation, toString is called on this value during concatenation) 正确的值:NaN(因为运算符是字符串连接,所以在连接期间在此值上调用toString)

Result: 'baNaN' 结果:“ baNaN”


Step 3: 'baNaN' + 'a' 步骤3: 'baNaN' + 'a'

Left Expression: 'baNaN' 左表达: 'baNaN'

Left Value: 'baNaN' 左值:“ baNaN”

Operator: + (one of the expression sides is a string, so string concatenation) 运算符:+(表达式的一面是字符串,因此是字符串连接)

Right Expression: 'a' 正确的表达方式: 'a'

Right Value: 'a' 正确的值:“ a”

Result: 'baNaNa' 结果:“ baNaNa”


After this the grouping expression has been evaluated, and toLowerCase is called which leaves us with banana. 此后,对分组表达式进行了评估,并调用了toLowerCase,这使我们有了香蕉。

转载地址:http://cicnb.baihongyu.com/

你可能感兴趣的文章
Gitlab 更新后gitlab-ctl reconfigure 提示 can only have one web server puma unicorn 错误
查看>>
videoc_streamon error 28, NO space left on device解决办法
查看>>
一个比make更好用的编译工具
查看>>
降低windows系统里面usb转串口模块延时的办法
查看>>
ROS 常用加速指令
查看>>
ubuntu ssh登入速度太慢的解决办法
查看>>
罕见bug解决办法: kienct 1代运行错误Failed to claim camera interface: LIBUSB_ERROR_NOT_FOUND
查看>>
rviz的简单使用
查看>>
解决ROS的usb_cam节点无法正常读取mjpeg格式摄像头的方法
查看>>
Ubuntu VNC 如何调整分辨率
查看>>
病毒编程技术-3
查看>>
病毒编程技术-4
查看>>
病毒编程技术-5
查看>>
第9周上机实践项目1——利用循环求和
查看>>
第9周上机实践项目2——分数的累加
查看>>
第9周上机实践项目3——输出星号图
查看>>
第9周上机实践项目4——乘法口诀表
查看>>
第9周上机实践项目5——程序填充题
查看>>
第9周上机实践项目6——穷举法解决组合问题(1~3)
查看>>
第9周上机实践项目6——穷举法解决组合问题(3~7)
查看>>