1.函数默认参数
2.剩余参数(3种写法)
function sum(message){
// let args = Array.prototype.slice.call(arguments) // ES5 将伪数组变为真数组
// let args = Array.from(arguments) // ES6 将伪数组变为真数组
let args = [...arguments] // ES6 将伪数组变为真数组
let numbers = args.slice(1)
result = numbers.reduce((p,v)=>p+v,0)
return message + result
}
sum('结果是',1,2,3,4,5,6,7,8,9,10)
function sum(message,...numbers){
result = numbers.reduce((p,v)=>p+v,0)
return message + result
}
sum('结果是',1,2,3,4,5,6,7,8,9,10)
3.展开操作
let array1 = [1,2,3,4,5,6]
let [a,b,c,...array2] = array1
console.log(array2)
打印出的结果为[4,5,6]
var array1 = [1,2,3,4,5,6]
var [,,,...array2] = array1
console.log(array2)
打印出的结果还是为[4,5,6]
var array1 = [1,2,3,4,5,6]
var [...array2] = array1
console.log(array2)
打印出的结果为[1,2,3,4,5,6]
var array1 = [1,2,3,4,5,6]
var array2 = [0,...array1,7]
console.log(array2)
打印出的结果为[0,1,2,3,4,5,6,7]
4.解构赋值
var a = 1
var b = 2; //分号在文件里不可以省略,在控制台里面可以省略
[a,b] = [b,a]
console.log(a)
console.log(b)
[a, b, ...rest] = [10, 20, 30, 40, 50]
let {name, age} = frank
解构赋值
解构赋值
解构赋值
解构赋值
解构赋值
解构赋值
解构赋值,把name重命名为xinming,如果没取到xinming,那默认值为Tom
浅拷贝
浅拷贝
对象合并
对象合并
5.对象属性加强
对象属性加强
var x = 1
var y = 2
var obj1 = {name:1,age:2}
var obj2 = {
z:1,
x,
y,
...obj1,
sayHi(){},
sayBye(){},
sayByeBye:function(){
}
}
console.log(obj2)
属性增强
image.png
网友评论