你知道 JavaScript 这些奇怪的部分吗?
什么?!你说 JavaScript 很奇怪?拜托,兄弟,我已经用它好几年了,它是我见过的最好的语言。
我们喝杯咖啡,看看我们是否可以在下次采访中问一些这些问题,呵呵。
开始了:
> 0.1 + 0.2
0.30000000000000004
> 1 + “23” - 10
“113”
> null == undefined
true
> NaN == NaN
false
> typeof null
"object"
> typeof NaN
"number"
> ['1', '7', '11'].map(parseInt)
(3) [1, NaN, 3]
> function foo() {
return
{
foo: 'bar'
}
}
foo()
< undefined
> function bar() {
return {
foo: 'bar'
}
}
bar()
> {foo: "bar"}
> [] + []
""
> {} + {}
"[object Object][object Object]"
> [] + {}
"[object Object]"
> {} + []
0
> +!+[]
1
3 > 2
true
3 > 2 > 1
false
> function Test() {
}()
Uncaught SyntaxError: Unexpected token )
> var test = function() {}
undefined
参考:
https://blog.mgechev.com/2013/02/22/javascript-the-weird-parts
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators
文章来源:https://dev.to/lqj/do-you-know-these-weird-parts-of-javascript-48pg