本文翻译自:Why is the result of ('b'+'a'+ + 'a' + 'a').toLowerCase() 'banana'?

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楼

参考:https://stackoom.com/question/3t4yi/为什么-b-a-a-a-toLowerCase-banana-的结果


#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 similar puns in JavaScript that you can check out. 您可以签出JavaScript中许多类似的双关语 。


#4楼

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

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

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

(see: operator precedence ) (请参阅: 运算符优先级 )

(+'a') attempts to convert 'a' to a number using the unary plus operator . (+'a')尝试使用一元加运算符将'a'转换为数字。 Because 'a' is not a number, the result is NaN ( "Not-A-Number" ): 因为'a'不是数字,所以结果为NaN“ 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')仅由字符串文字和加法运算符组成。

  • String Literals "A string literal is zero or more characters enclosed in single or double quotes." 字符串文字 “字符串文字是用单引号或双引号引起来的零个或多个字符。”
  • The Addition operator ( + ) "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 the String Type "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,这使我们有了香蕉。

为什么(#39;b#39;+#39;a#39;+ +#39;a#39;+#39;a#39;)。toLowerCase()#39;banana#39;的结果?相关推荐

  1. appium+python自动化45-夜神模拟器连不上(adb server version (36) doesn't match this client (39); killing...)...

    前言 最新下了个最新版的夜神模拟器,然后adb devices发现连不上模拟器了,报adb server version (36) doesn't match this client (39); ki ...

  2. centos 编译内核至2.6.39.4

    一.下载内核文件并解压至相应目录 # wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.39.4.tar.bz2 # tar jxv ...

  3. 【LeetCode】《剑指Offer》第Ⅴ篇⊰⊰⊰ 39 - 47题

    [LeetCode]<剑指Offer>第Ⅴ篇⊰⊰⊰ 39 - 47题 文章目录 [LeetCode]<剑指Offer>第Ⅴ篇⊰⊰⊰ 39 - 47题 39. 数组中出现次数超过 ...

  4. 吉林大学计算机学院男女,最狠盘点:39所985大学男女比例排行,23名开外请自备女票入学...

    原标题:最狠盘点:39所985大学男女比例排行,23名开外请自备女票入学 39所985高校重排,不过这次不是拼实力,而是拼男女比例.看看哪些学校女生都是稀有动物,哪些学校光棍最多. 第一档,男生比例3 ...

  5. 明美新能在创业板IPO过会:计划募资4.5亿元,2022年营收约39亿元

    2月17日,深圳证券交易所披露的信息显示,广州明美新能源股份有限公司(下称"明美新能")获得上市委会议审议通过.本次冲刺上市,明美新能计划募资4.50亿元,德邦证券为其保荐机构. ...

  6. 递归算法—第39级台阶

    递归算法 基本思想: 递归算法一般用于将较为复杂的规模较大的问题分解成规模较小的同类问题,通过函数或子过程直接或间接调用自身,以小规模问题解决复杂问题,使复杂问题简单化,使程序更加简洁,是解决很多计算 ...

  7. 震惊!谁上春晚最多?我用 Python 分析过往 39 届央视春晚的数据

    一年又一年,看过那么多春晚,哪一年.哪些节目.哪些人你还留有深刻印象呢.如今,距离第一届春晚 1983 年,整整过去了 39 年.谁导演春晚次数最多,谁主持春晚次数最多,谁上春晚次数最多?今天小编用 ...

  8. 剑指offer:面试题39. 数组中出现次数超过一半的数字

    题目:面试题39. 数组中出现次数超过一半的数字 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. 你可以假设数组是非空的,并且给定的数组总是存在多数元素. 示例 1: 输入: [1, ...

  9. 【蓝桥java】递归基础之39级台阶

    题目: 小明刚刚看完电影<第39级台阶>.离开电影院的时候,他数了数礼堂前的台阶数,恰好是39级! 站在台阶前,他突然又想着一个问题: 如果我每一步只能迈上1个或2个台阶.先迈左脚,然后左 ...

最新文章

  1. JS如何获取RadiobuttonList的选中值
  2. MindMaster Pro中文版
  3. 【重复制造精讲】Backflush 倒冲介绍
  4. python中messagebox用法实例_pyqt4教程之messagebox使用示例分享
  5. 计算机英语A卷答案,计算机专业英语试题及答案A卷.doc
  6. IoT日志利器:嵌入式日志客户端(C Producer)发布
  7. Whl自助搜索下载器
  8. Harmony OS — Text文本框
  9. 2017c语言国二试题,国家计算机c语言二级考试试题
  10. Python自制成语接龙小游戏
  11. windows系统磁盘空间清理工具SDelete
  12. 终面(HR面)_职业竞争力和职业规划
  13. 高级验证方法学()-Mentor-笔记
  14. 支付宝,百度,头条集卡群,过年一起玩
  15. vue项目微前端试水
  16. LE Audio问世!蓝牙5.2加持的TWS耳机打破AirPods专利垄断现状
  17. 交换网络基础-交换机的工作原理
  18. Linux:生成core的几种方式
  19. 【BLE】CC2541之SBL
  20. Python基础知识笔记

热门文章

  1. java Serializable 详解
  2. linux中关闭报警音
  3. 配置Yarn-Resourcemanager HA
  4. iOS安全之class-dump的安装和使用
  5. c++ opencv实现区域填充_使用OpenCV实现图像覆盖
  6. java webservice接口开发_搭建Soap webservice api接口测试案例系统
  7. 从unmarshal带json字符串字段的json说起
  8. Linux服务器出现java.net.UnknownHostException 异常处理
  9. Java小程序—录屏小程序(上半场)
  10. 454. 4Sum II