zoukankan      html  css  js  c++  java
  • 啥? ++[[]][+[]]+[+[]] = 10?

    咱先把这个表达式分开:

    ++[[]][+[]]
    +
    [+[]]

    在JavaScript中, 以下等式是true +[]===0+ 会把一些字符转换成数字, 在这里它会变成 +"" or 0 (具体规则例如以下).

    咱能够把它简化成这样 (++ 的优先级比 +更高):

    ++[[]][0]
    +
    [0]

    因此 [[]][0] 意味着: 获取 [[]] 的第一个元素,实际上:

    • [[]][0] 返回内部的数组 ([]). 依据语言规范,说 [[]][0]===[]是不正确的,咱把内部这个数组称为 A 以免误会.
    • ++[[]][0]==A+1, 由于 ++ 意味着 '加1'.
    • ++[[]][0]===+(A+1); 换句话说,这玩意儿得到的永远是个数值 (+1 不一定返回一个数值, 但++ 一定是).

    咱能够让表达式更简化. 用 [] 替换 A:

    +([] + 1)
    +
    [0]

    在JavaScript中, 这个等式也是true: []+1==="1", 由于[]=="" (连接一个空数组), so:

    • +([]+1)===+(""+1), and
    • +(""+1)===+("1"), and
    • +("1")===1

    还能够更简化:

    1
    +
    [0]

    在 JavaScript中这个等式 [0]=="0", 也是true,由于这相当于一个  有一个元素的数组内部元素的连接。连接各元素使用 ,切割,当仅仅有一个元素时,能够推出结果就是这个元素本身。

    故此, 我们终于得出 (number + string = string):

    1
    +
    "0"
    
    ==="10" // 耶!

     +[]的具体规则例如以下:

     对于 +[], 首先会被转化成一个 string 由于这是 + 规定的:

    11.4.6 一元运算符 + 

     + 运算符会把操作数转化成 Number 类型.

    以下这个一元表达式 : + 一元表达式   会进行例如以下操作:

    1. 令 expr 为一元表达式的结果.

    2. 返回 ToNumber(GetValue(expr)).

    ToNumber() :

    Object

    应用例如以下步骤:

    1. 令 primValue 为 ToPrimitive(input argument, hint String).

    2. 返回 ToString(primValue).

    ToPrimitive() :

    Object

    返回这个Object的默认值。默认值咋算呢,通过调用 [[DefaultValue]] 这个 object的内部方法, passing the optional hint PreferredType.  [[DefaultValue]] 方法定义见 ECMAScript objects 中 8.12.8.

    [[DefaultValue]] :

    8.12.8 [[DefaultValue]] (hint)

    object O内部方法 [[DefaultValue]] 被调用时, 会运行例如以下操作: (不翻译了,各位自己感兴趣的话自己看吧)

    1. Let toString be the result of calling the [[Get]] internal method of object O with argument "toString".

    2. If IsCallable(toString) is true then,

    a. Let str be the result of calling the [[Call]] internal method of toString, with O as the this value and an empty argument list.

    b. If str is a primitive value, return str.

    The .toString of an array says:

    15.4.4.2 Array.prototype.toString ( )

    When the toString method is called, the following steps are taken:

    1. Let array be the result of calling ToObject on the this value.

    2. Let func be the result of calling the [[Get]] internal method of array with argument "join".

    3. If IsCallable(func) is false, then let func be the standard built-in method Object.prototype.toString (15.2.4.2).

    4. Return the result of calling the [[Call]] internal method of func providing array as the this value and an empty arguments list.

    So +[] comes down to +"", because [].join()==="".

    Again, the + is defined as:

    11.4.6 Unary + Operator

    The unary + operator converts its operand to Number type.

    The production UnaryExpression : + UnaryExpression is evaluated as follows:

    1. Let expr be the result of evaluating UnaryExpression.

    2. Return ToNumber(GetValue(expr)).

    ToNumber is defined for "" as:

    The MV of StringNumericLiteral ::: [empty] is 0.

    So +""===0, and thus +[]===0.

    原文
  • 相关阅读:
    Unity 3D Customizing Your Workspace 自定义工作区
    Unity3D 学习界面 Learning the Interface
    【Python】函数功能描述
    没有显示器的情况下安装和使用树莓派
    【Linux】03-Linux用户权限相关命令
    【Linux】02-Linux远程管理常用命令
    【Linux】01-Linux基本命令
    【12c】DataGuard构建物理备用数据库(Cloud Control方式)
    【12c】一文教你详细了解如何安装Oracle Enterprise Manager Cloud Control 12c
    【12c】新特性:一文带你详细了解Oracle 12c 数据库的自动数据优化(ADO)
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4081884.html
Copyright © 2011-2022 走看看