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; } ?>javascript - es6 how to use default parameters that go before non-default parameters? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - es6 how to use default parameters that go before non-default parameters? - Stack Overflow

programmeradmin4浏览0评论

I am a bit rusty on default parameters, and I am wondering how can I use a default value for a parameter if it goes before parameters without defaults?

In the example from Redux.js below, when will the default value {} for the state parameter be useful? (since you can't default the next parameter)?

const todo = (state = {}, action) => {
  switch (action.type) {
    //...

    case 'TOGGLE_TODO':
      if (state.id !== action.id) {
        return state
      }

      return Object.assign({}, state, {
        pleted: !statepleted
      })

    default:
      return state
  }
}

I am a bit rusty on default parameters, and I am wondering how can I use a default value for a parameter if it goes before parameters without defaults?

In the example from Redux.js below, when will the default value {} for the state parameter be useful? (since you can't default the next parameter)?

const todo = (state = {}, action) => {
  switch (action.type) {
    //...

    case 'TOGGLE_TODO':
      if (state.id !== action.id) {
        return state
      }

      return Object.assign({}, state, {
        pleted: !state.pleted
      })

    default:
      return state
  }
}
Share Improve this question edited Jun 12, 2017 at 8:58 thor asked Jun 12, 2017 at 4:42 thorthor 22.5k31 gold badges102 silver badges191 bronze badges 5
  • 6 It makes no sense. The default parameters must e last. For that reason in some piled languages it's even denied. – zerkms Commented Jun 12, 2017 at 4:47
  • Why can't you "default the next parameter"? – RobG Commented Jun 12, 2017 at 4:57
  • @RobG I didn't e up with the example. I am just trying to understand a use case in Redux tutorial. – thor Commented Jun 12, 2017 at 9:00
  • @zerkms see my answer below to understand the redux use case. – cquezel Commented Mar 8, 2018 at 17:27
  • @cquezel my ment was not about whether it technically is possible or not, but about the design perspective of doing so. – zerkms Commented Mar 8, 2018 at 19:02
Add a ment  | 

4 Answers 4

Reset to default 8

The usage in question is specific to redux.js. The default value for the first parameter is generally useless in function calls because of the second parameter without default.

However, as said earlier in the same tutorial about Reducers:

Redux will call our reducer with an undefined state for the first time. This is our chance to return the initial state of our app:

function todoApp(state, action) {
  if (typeof state === 'undefined') {
    return initialState
  }
  //...
  return state
}

So the 1st parameter isn't really omitted here. Redux is supplying undefined as its value on initialization. It is only in this case, the tutorial used default arguments syntax as a shortcut:

function todoApp(state = initialState, action) {
  //...
  return state
}

The defaults are called when the parameter is undefined:

todo(undefined, { type: 'WHATEVER' });

To prevent the need for setting undefineds when calling the function, I prefer to destructure an object with defaults. Using an object make the order of the params irrelevant.

todo({ state = {}, action } = {}) => {};

Default parameter has to e last i dont think there is a direct way to make them e before other parameters however you can use Arguments Object to achieve something like this.

e.g

function myFunction(){
  var firstParam , secondParam;
  if(arguments.length === 0){
     console.log("no input");
     return;
  }
  if(arguments.length === 1){
    secondParam =  arguments[0];
  }
  else{
    firstParam  =  arguments[0];
    secondParam =  arguments[1];
  }
   // you can write any logic above
   // also you can give params in function definition as well myFunction(firstParam , secondParam)
   // use params as you wish
  console.log(firstParam);
  console.log(secondParam);
}

I'm a newbe to javascript but if I am not mistaken, default parameters simply replace 'undefined' parameters.

if I have a function defined as:

function example(var1 = false, var2, var3 = false) ...

This means that the following calls are all legal :

example(true, someVar, true);
example(true, someVar); // calls  example(true, someVar, false)
example(undefined, someVar, true); // calls example(false, someVar, true)
example(undefined, someVar); // calls example(false, someVar, true)

The redux framework simply passed undefined explicitly.

发布评论

评论列表(0)

  1. 暂无评论