首页 > Web开发 > 详细

JS面试Q&A(续2): Rest parameter,Arrow function 等

时间:2017-09-30 09:12:20      阅读:282      评论:0      收藏:0      [点我收藏+]

rest parameter 和 Destructuring assignment.

function fun1(...theArgs) {
console.log(theArgs.length);
}

// theArgs is an array

fun1(); // 0
fun1(5); // 1
fun1(5, 6, 7); // 3

function f(a, b, ...theArgs) {
// ...
}

function f(...[a, b, c]) {
return a + b + c;
}

f(1) // NaN (b and c are undefined)
f(1, 2, 3) // 6
f(1, 2, 3, 4) // 6 (the fourth parameter is not destructured)

见 ECMAScript 6 (2015)


Arrow function
* 简洁的语法,
* 没有自己的this, arguments, super 和 new.target.
特别适用于非属性函数,
不能用作构造函数.


function Person(){
this.age = 0;

setInterval(() => {
this.age++; // |this| properly refers to the person object
}, 1000);
}

var p = new Person();

  

 

JS面试Q&A(续2): Rest parameter,Arrow function 等

原文:http://www.cnblogs.com/GameEngine/p/7613491.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!