问题描述
如果我说let 5 = 10
,为什么5 + 1
返回6
而不是11
?
If I say let 5 = 10
, why does 5 + 1
return 6
instead of 11
?
推荐答案
当你说
let 5 = 10
它不是5的重定义,是模式匹配,与您说的相同
it's not a redefinition of 5, it's a pattern matching, the same which occurs when you say
foo 5 = undefined
... foo 10 ...
该模式一旦匹配就会失败.
The pattern simply fails if it's ever matched.
在let表达式中,匹配是惰性的.这意味着仅在评估由其绑定的变量时才进行匹配.这样我们就可以写类似
In let-expressions the match is lazy. This means the match is only being done when a variable bound by it is evaluated. This allows us to write things like
let foo = undefined in 10
表达式中没有变量绑定,因此模式永远不会匹配.
In your expression, no variable is bound, so the pattern is never matched.
可以说,没有变量的这种模式在let绑定中没有意义,应该由编译器检测,但是语言并不禁止它们.
Arguably such patterns with no variables make no sense in let-bindings and should be detected by the compiler, but the language doesn't forbid them.
这篇关于“让5 = 10"是做什么的?这不是分配操作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!