动漫引擎

导航

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

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();

  

 

posted on 2017-09-30 06:35  动漫引擎  阅读(176)  评论(0编辑  收藏  举报