测试JS Variable是否设置为函数参数

时间:2011-10-25 20:14:26

标签: javascript

测试javascript变量是否将值传递给函数

的最佳方法是什么
function test(a, b) {  /* check if b was given a value */ }

我想说

if(!b)

但如果b = 0这不起作用。我是否必须单独检查它是否未定义或为空

if(typeof(b) === 'undefined' || b === null)

还是有更好的方式来写这个吗?

2 个答案:

答案 0 :(得分:2)

你可以查看参数属性

if(arguments.length > 1){ //is there a 'b' argument
 //do stuff
}

答案 1 :(得分:0)

我相信您只需检查undefined:

if (b === undefined)

请参阅http://www.w3schools.com/jsref/jsref_undefined.asp

相关问题