te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>How to set default values for parameters of a JavaScript function and call it with less parameters? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to set default values for parameters of a JavaScript function and call it with less parameters? - Stack Overflow

programmeradmin3浏览0评论

I have a function

function test(name)
{

}

I call it with test("kanishka");

In one case, I want to pass two parameters (name and age) to this function:

test("kanishka",27);

In PHP, I can do this by setting a default value to age :

test(name , age = NULL)

Is there any way in JavaScript to do this?

I tried

test(name , age = NULL) 

but it gives errors.

I can declare 2 functions

test(name) and  test(name , age = NULL)

but it duplicates the code.

Or I can change my previous function call which took one parameter to two parameters, giving a default value for age:

test("kanishka" , 0)

but in this case I have to find all function call and change them.

I have a function

function test(name)
{

}

I call it with test("kanishka");

In one case, I want to pass two parameters (name and age) to this function:

test("kanishka",27);

In PHP, I can do this by setting a default value to age :

test(name , age = NULL)

Is there any way in JavaScript to do this?

I tried

test(name , age = NULL) 

but it gives errors.

I can declare 2 functions

test(name) and  test(name , age = NULL)

but it duplicates the code.

Or I can change my previous function call which took one parameter to two parameters, giving a default value for age:

test("kanishka" , 0)

but in this case I have to find all function call and change them.

Share Improve this question edited Dec 28, 2011 at 10:02 nnnnnn 150k30 gold badges209 silver badges247 bronze badges asked Dec 28, 2011 at 9:50 Kanishka PanamaldeniyaKanishka Panamaldeniya 17.6k31 gold badges127 silver badges194 bronze badges 4
  • 1 possible duplicate of Handling optional parameters in javascript – Felix Kling Commented Dec 28, 2011 at 9:54
  • 1 This question doesn't have anything to do with jQuery, it is only about JavaScript. – Douglas Commented Dec 28, 2011 at 9:56
  • Regarding I can declare 2 functions: No, you can't JavaScript does not support function overloading. If you declare two functions with the same name, the latter will override the former. – Felix Kling Commented Dec 28, 2011 at 10:03
  • 1 "I can declare 2 functions" - Not with the same name and scope you can't. You can give them different names. "but it duplicates the code" - Not necessarily, you can put all the code in the function with both params and then the other function will simply call it with the default value for the second param. However, you don't need to do that at all, see the answers below. – nnnnnn Commented Dec 28, 2011 at 10:07
Add a ment  | 

6 Answers 6

Reset to default 8

Just changing the definition of the function will work

function test(name, age)
{

}

All existing uses of the function will assign to age an undefined value..

Whenever you use a second parameter it will be assigned to the age parameter.

If you want a default value change the function to

function test(name, age)
{
    age = (age === undefined) ? 0 : age; // change 0 to the default value you want..
}

Update

There is now support for default parameters

function test(name, age = 0)
{
  // age will be 0 if no second parameter is provided or it is undefined
}

basically you will do something like this

function foo(a, b)
 {
   a = typeof(a) != 'undefined' ? a : 42;
   b = typeof(b) != 'undefined' ? b : 'default_b';
   ...
 }

You can still call the method, even if you pass the wrong number of arguments. The "optional" argument will be set to undefined, which you can check for using an if statement.

You can either declare the function with the 2 arguments:

function test(name, age) {
   //...
}
test("James"); //age will be undefined
test("James", 22); //age will be 22

Or you could forget about the parameters altogether and use the arguments object:

function test() {
    console.log(arguments[0]);
}

This way you can pass in as many arguments as you like, and access them through the arguments object.

You can use the arguments object which holds all arguments passed to a function - regardless of if they are defined or not...

test(name){
    var age = arguments[1] == undefined ? 0 : arguments[1]
    // do stuff here
}

The "standard" way to provide default values for methods is to check whether the arguments have been supplied at the start of the method body:

function test(name, age)
{
  age = typeof(age) != 'undefined' ? age : NULL;
  ...
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论