Skip to content

Instantly share code, notes, and snippets.

@Gerhut
Created June 19, 2014 03:56
Show Gist options
  • Select an option

  • Save Gerhut/4186b23fceb6971d3658 to your computer and use it in GitHub Desktop.

Select an option

Save Gerhut/4186b23fceb6971d3658 to your computer and use it in GitHub Desktop.
{}的二义性
{} + [] // => {}; +[] => 0
[] + {} // => '' + '[object Object]' => '[object Object]'
{} + [] == [] + {} // => {}; 0 == '[object Object]' => false
({} + [] == [] + {}) // => '[object Object]' + '' == '' + '[Object Object]' => true
/*
知识点:
1. {}放在语句开头的时候,被当做括代码的大括号;不在开头的时候,被当做括对象的大括号。
2. 单目运算符 `+ val` 就是把val强转为数字
3. +两边只要有非数字,就把两边转为字符串,做字符串连接。
4. 空数组转为空字符串,空对象转为'[object Object]'
*/
@mingyun

mingyun commented Jun 21, 2014

Copy link
Copy Markdown

({}) + [] '[object Object]'
{}+{} NaN

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment