javascript:为什么要“符号前缀"?一个变量? [英] javascript: why to "sign-prefix" a variable?

查看:26
本文介绍了javascript:为什么要“符号前缀"?一个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在查看一个小的 3rd-party javascript 库,我看到其中有很多带符号前缀"的变量:

function_call(+value);

我知道,你可以交换符号,如果你用-"前缀一个变量,但为什么用+"前缀 - 它对值没有任何影响,不是吗?

提前致谢!

解决方案

+ 强制 value 成为数字.

<预><代码>>>>1"1">>>+"1"1

i'm currently looking at a little 3rd-party javascript library and i see a lot of "sign-prefixed" variables in it:

function_call(+value);

i know, that you can swap the sign, if you prefix a variable with '-', but why to prefix something with a '+' -- it doesn't do anything to the value, no?

thanks in advance!

解决方案

The + forces the value to become a Number.

>>> "1"
"1"
>>> +"1"
1

这篇关于javascript:为什么要“符号前缀"?一个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆