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

本文详细介绍了ECMAScript 6中Rest参数(restparameter)和解构赋值(Destructuring assignment)的使用方法,并通过具体示例展示了如何利用这些特性来简化函数参数的处理。此外还探讨了箭头函数的简洁语法特点及其适用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

  

 

转载于:https://www.cnblogs.com/GameEngine/p/7613491.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值