码迷,mamicode.com
首页 > 其他好文 > 详细

[ES6] ES6 Parameter Object Destructuring with Required Values

时间:2016-08-09 02:04:21      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

Not only can you provide default values when using ES6 parameter object destructuring, but you can also require the presence of certain properties.

 

function ajax({
  type = "get",
  url = requiredParameter("url"),
  data = {},
  success = requiredParameter("success"),
  error = () => {},
  isAsync = true } = {}) {
    console.log(JSON.stringify({ type, url, data, success, error, isAsync }, null, 2));
}

function requiredParameter(name){
  console.log(`parameter missing: "${name}"`);
}

try{
 ajax({url: http://api.com, data: {name: Zhentian}, success: false}) 
}catch(e){
  console.log(JSON.stringify(e))
}

 

Now, success and url are rueqired, if we don‘t passin success, then it will show:

//"parameter missing: \"success\""

 

[ES6] ES6 Parameter Object Destructuring with Required Values

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5751568.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!