zoukankan      html  css  js  c++  java
  • Implicitly: Strings <--> Numbers

    According to ES5 spec section 11.6.1, the + algorithm (when an object value is an operand) will concatenate if either operand is either already a string, or if the following steps produce a string representation. So, when + receives an object (including array) for either operand, it first calls the ToPrimitive abstract operation (section 9.1) on the value, which then calls the [[DefaultValue]] algorithm (section 8.12.8) with a context hint of number.

    If you're paying close attention, you'll notice that this operation is now identical to how the ToNumber abstract operation handles objects (see the "ToNumber"" section earlier). The valueOf() operation on the array will fail to produce a simple primitive, so it then falls to a toString() representation. The two arrays thus become "1,2" and "3,4", respectively. Now, + concatenates the two strings as you'd normally expect: "1,23,4".

    var a = [1,2];
    var b = [3,4];
    
    a + b; // "1,23,4"
    

    simplified explanation: if either operand to + is a string (or becomes one with the above steps!), the operation will be string concatenation. Otherwise, it's always numeric addition.

  • 相关阅读:
    Python学习笔记 for windows 二
    Socket.io:有点意思
    MEAN Stack:创建RESTful web service
    在EC2上安装MEAN环境
    NodeJS:树的序列化
    NodeJS:树的反序列化
    NodeJS学习:爬虫小探补完计划
    NodeJS学习:爬虫小探
    依赖包bcrypt安装Issues
    Mongoose:Schema之路
  • 原文地址:https://www.cnblogs.com/zlv2snote/p/10759780.html
Copyright © 2011-2022 走看看