1.多行字符串
let str = '
hello,
My name is Selina.
Nice to meet you.
'
str // " hello,My name is Selina.Nice to meet you."
2.字符串模板
let names = 'Selina'
let who = 'you'
let str = `Hi
My name is ${names}.
Nice to meet ${who}
`
str // "Hi
My name is Selina.
Nice to meet you.
用$+{}来实现
3.数组
var a = [1, 2]
console.log(...a) //1,2
var b = [...a, 3]
b // [1, 2, 3]
...相当于把外面的[]拿掉,里面就是1,2了。
function sort(...arr){
console.log(arr.sort())
}
sort(4,3,8) //[3,4,8]
得到最大的值
function sum(arr){
return Math.max(...arr)
}
sum([1,2,3])
参数不是数组,所以用去掉外面[]方法。
4.函数
默认值
function sayHi(name='Tom') {
console.log(`Hi, ${name}`)
}
sayHi() //Hi,Tom
sayHi('Selina') //Hi,Selina
用法和
这个例子看懂
//ex1
function m1({x = 0, y = 0} = {}) {
return [x, y];
}
//ex2
function m2({x, y} = { x: 0, y: 0 }) {
return [x, y];
}
// 函数没有参数的情况
m1() // [0, 0]
m2() // [0, 0]
// x 和 y 都有值的情况
m1({x: 3, y: 8}) // [3, 8]
m2({x: 3, y: 8}) // [3, 8]
// x 有值,y 无值的情况
m1({x: 3}) // [3, 0]
m2({x: 3}) // [3, undefined]
// x 和 y 都无值的情况
m1({}) // [0, 0];
m2({}) // [undefined, undefined]
m1({z: 3}) // [0, 0]
m2({z: 3}) // [undefined, undefined]
5.对象
var name = 'Tom'
var age = 18
var p = {name, age}
p //{name: 'Tom', age: 18}
补充一个
var App = {
init:function(){
console.log(1)
},
bind: function(){
}
}
App.init()
等于
var App = {
init(){
console.log(1)
},
bind(){
}
}
App.init()