[英]Give integers a default value if they are initialized as empty
I have this (it's a Node.js addon) 我有这个(这是一个Node.js插件)
void Method(const v8::FunctionCallbackInfo<v8::Value>& args) {
int count = args[2]->ToNumber()->NumberValue();
int priority = args[3]->ToNumber()->NumberValue();
int priority_search_cap = args[4]->ToNumber()->NumberValue();
cout << " count => " << count << endl;
cout << " priority => " << priority << endl;
cout << " priority_search_cap => " << priority_search_cap << endl;
}
what I get logged out is: 我注销的是:
count => -2147483648
priority => -2147483648
priority_search_cap => -2147483648
What I would like to do is give these variables default values in case no values are passed to properly initialize them? 我想做的是为这些变量提供默认值,以防万一没有传递任何值来正确初始化它们? (Meaning, args[2], args[3], args[4] are undefined.)
(意味着,args [2],args [3],args [4]未定义。)
How can I give these integers default values? 如何给这些整数提供默认值?
the Value
type has a nice IsUndefined()
method to check this Value
类型有一个不错的IsUndefined()
方法来检查
You could do this using a ternary expression to set value 0 in case of undefined value: 如果值未定义,则可以使用三元表达式将值设置为0:
int count = args[2]->IsUndefined() ? 0 : args[2]->ToNumber()->NumberValue();
int priority = args[3]->IsUndefined() ? 0 : args[3]->ToNumber()->NumberValue();
int priority_search_cap = args[4]->IsUndefined() ? 0 : args[4]->ToNumber()->NumberValue();
check Value
API here: http://bespin.cz/~ondras/html/classv8_1_1Value.html#aea287b745656baa8a12a2ae1d69744b6 在此处检查
Value
API: http : //bespin.cz/~ondras/html/classv8_1_1Value.html#aea287b745656baa8a12a2ae1d69744b6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.