简体   繁体   English

为什么在lodash中使用`+ start`而不是`start`

[英]why use `+start` instead of `start` in lodash

https://github.com/lodash/lodash/blob/3.7.0/lodash.src.js#L2781 https://github.com/lodash/lodash/blob/3.7.0/lodash.src.js#L2781

What does +start mean in this snippet start = start == null ? 0 : (+start || 0); +start在此代码段中是什么意思start = start == null ? 0 : (+start || 0); start = start == null ? 0 : (+start || 0); ?

In my opinion, +start equals 0+start . 我认为+start等于0+start and we'v already known start is a number. 而且我们已经知道start是一个数字。

so why not use start = start == null ? 0 : (start || 0); 那么为什么不使用start = start == null ? 0 : (start || 0); start = start == null ? 0 : (start || 0); ?

Is there anything I misunderstand.? 我有什么误会吗? I am really confused. 我真的很困惑。

You don't know, at the start of that method, that start is a number. 您不知道该方法的start是数字。 The documentation says it ought to be , but when has the documentation always been exactly right? 文档说应该是 ,但是什么时候文档总是正确无误?

There isn't any real difference between +start and 0+start , as both will coerce start into a number (so will -start , but it obviously inverts the sign in the process). +start0+start之间没有任何实际区别,因为两者都会将start强制转换为数字( -start也将强制转换为数字,但显然会在过程中反转符号)。

Assuming the docs are right and start is already a number, +start is a no-op and doesn't cause any problems or break anything. 假设文档是正确的,并且start已经是一个数字, +start是无操作,并且不会引起任何问题或破坏任何东西。 If start is not a number (the next most likely type is probably string), then this will coerce it into a number before using it. 如果start不是数字(下一个最可能的类型可能是字符串),那么它将在使用前将其强制为数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM