We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给函数赋值的类似操作很常见:
function sure(options) { let name = options.name || 'qize' let age = options.age || 25 // ... }
这样的赋值没有什么大问题,但是如果遇到下面这种情况就会有问题了。比如,age 就是 0,且这个值就是合法的,如果按照上面的逻辑,这里还是会显示 25,很显然,这不是我们想要的结果。
age
0
25
所以这里,正确且安全的做法是使用 typeof 进行参数的类型检查
typeof
function sure(options) { let name = (typeof options.name !== 'undefined') ? options.name : 'qize' let age = (typeof options.age !== 'undefined') ? options.age || 25 // ... }
其实这里容易出现问题不只是 0,还有 空字符串等 Falsy 值。
Falsy
Falsy包括:false、undefined、null、正负0、NaN、""
The text was updated successfully, but these errors were encountered:
No branches or pull requests
给函数赋值的类似操作很常见:
这样的赋值没有什么大问题,但是如果遇到下面这种情况就会有问题了。比如,
age
就是0
,且这个值就是合法的,如果按照上面的逻辑,这里还是会显示25
,很显然,这不是我们想要的结果。所以这里,正确且安全的做法是使用
typeof
进行参数的类型检查其实这里容易出现问题不只是
0
,还有 空字符串等Falsy
值。The text was updated successfully, but these errors were encountered: