admin管理员组文章数量:1415111
I have a function in javascript :
function test(a, b, c) {
if(typeof b == "undefined")
//do something
//function code
}
Now i want to call this function in such a way so that typeof b remains undefined
and a & c containes
value (without reordering a
, b
& c
), like
test("value for a", what i can pass here so that b type will be undefined, "value for c")
I have a function in javascript :
function test(a, b, c) {
if(typeof b == "undefined")
//do something
//function code
}
Now i want to call this function in such a way so that typeof b remains undefined
and a & c containes
value (without reordering a
, b
& c
), like
test("value for a", what i can pass here so that b type will be undefined, "value for c")
Share
Improve this question
edited Jun 12, 2012 at 9:24
ThiefMaster
319k85 gold badges607 silver badges646 bronze badges
asked Jun 12, 2012 at 9:14
King KongKing Kong
2,9155 gold badges29 silver badges39 bronze badges
1
- 2 Create a variable without value and use it or maybe even a non existing variable. – Michael Laffargue Commented Jun 12, 2012 at 9:16
4 Answers
Reset to default 9Simply pass undefined
(without quotes):
test("value for a", undefined, "value for c");
Any variable (that's undefined).
var undefinedVar;
test("value for a", undefinedVar, "value for b");
I would suggest another approach if you know that you either pass a,b and c or you pass a and c. Then do as follows
function test(a, b, c) {
if (arguments.length < 3){
c = b;
b = arguments[2]; //undefined
//do want ever you would do if b is undefined
}
}
In this case if you by mistake pass an undefined for b it's easier to spot because it's not interpreted as "undefined actually doesn't mean undefined but is a flag telling me to do something different" it's usually more robust to test the arguments length than to rely on the value of the parameters especially if that value can also be the result of an error (Ie. if the value is undefined)
You can use void operator:
test('value for a', void 0, 'value for c');
void
operator evaluates the expression
and returns undefined
.
本文标签: jqueryCalling javascript function with undefined valueStack Overflow
版权声明:本文标题:jquery - Calling javascript function with undefined value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745212761a2647984.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论