# 模板字符串
| const date = new Date() |
| |
| console.log(`现在是北京时间: ${date.getFullYear()} 年 ${date.getMonth() + 1} 月 ${date.getDate()} 日`); |
| |
| |
| function getFullYear(){ |
| return date.getFullYear() |
| } |
| function getMonth(){ |
| return date.getMonth() + 1 |
| } |
| function getDate(){ |
| return date.getDate() |
| } |
| |
| console.log(`现在是北京时间: ${getFullYear()} 年 ${getMonth()} 月 ${getDate()} 日`); |
# 标签模板字符串
| function foo(name) { |
| console.log(`my name is saber`); |
| } |
| const name = 'saber' |
| foo`` |
| function foo(name, age) { |
| console.log('name', name, 'age', age); |
| console.log(`name: ${name} age: ${age}`); |
| } |
| const name = 'saber' |
| const age = 16 |
| |
| foo`saber 16` |
| function foo(name, age) { |
| console.log('name', name, 'age', age); |
| console.log(`name: ${name} age: ${age}`); |
| } |
| const name = 'saber' |
| const age = 16 |
| |
| foo`saber ${age} heixiuxiu~~ ` |
# 函数默认参数
| function foo(name = 'saber', age = 16) { |
| console.log(`name: ${name} age: ${age}`); |
| } |
| |
| foo() |
# 对象参数和默认值以及解构
| function foo({name, age } = {name: 'saber', age: 16}) { |
| console.log(`name: ${name} age: ${age}`); |
| } |
| |
| foo() |
# 传对象其中之一参数并赋默认值和解构
| function foo({name, age = 16} = {}) { |
| console.log(`name: ${name} age: ${age}`); |
| } |
| |
| foo({name: 'saber'}) |
# 有默认值的形参最好放在最后
| function sum(x, y, z = 30) { |
| return x + y + z |
| } |
| console.log(sum(10, 20)) |
# 默认值的形参放在前面解决方法
| function sum(z = 30, x, y) { |
| return x + y + z |
| } |
| console.log(sum(undefined ,10, 20)) |
| console.log(sum(0 ,10, 20)) |
| console.log(sum(null ,10, 20)) |
# 有默认值函数 length 属性 对默认值前面的参数属性 length 才有效
| function sum(w, x, y = 30, z) { |
| return w + x + y + z |
| } |
| console.log(sum.length) |
# 函数剩余参数
| function sum(x, ...args) { |
| console.log(x, args) |
| console.log(x, ...args) |
| } |
| sum(1, 2, 3, 4) |
- ES6 中引用了 rest parameter,可以将不定数量的参数放入到一个数组中:
- 如果最后一个参数是 ... 为前缀的,那么它会将剩余的参数放到该参数中,并且作为一个数组;
剩余参数和 arguments 有什么区别呢?
- 剩余参数只包含那些没有对应形参的实参,而 arguments 对象包含了传给函数的所有实参;
- arguments 对象不是一个真正的数组,而 rest 参数是一个真正的数组,可以进行数组的所有操作;
- arguments 是早期的 ECMAScript 中为了方便去获取所有的参数提供的一个数据结构,而 rest 参数是 ES6 中提供并且希望以此来替代 arguments 的;
- 剩余参数必须放到最后一个位置,否则会报错
# 箭头函数
- 箭头函数是没有显式原型的,所以不能作为构造函数,使用 new 来创建对象;
| const foo = () => { |
| |
| } |
| foo(1,2,3) |
| console.log(foo.prototype) |
# 展开语法
| const friends = ['薇尔莉特', '樱岛麻衣', '小鸟游六花'] |
| const saber = 'saber' |
| const lain = { |
| name: 'lain', |
| friends: ['saber'] |
| } |
| |
| |
| function foo(f1, f2, f3,) { |
| console.log(f1, f2, f3); |
| } |
| foo.apply(null, friends) |
| foo(...friends) |
| console.log(...saber) |
| |
| |
| const newFriends = [saber, ...friends] |
| console.log(newFriends) |
| |
| |
| const newLain = { ...lain, age: 16 } |
| console.log(newLain) |
# 展开语法 -> 浅拷贝
| const lain = { |
| name: 'lain', |
| friends: ['saber'] |
| } |
| |
| const newLain = { |
| ...lain, |
| age: 16 |
| } |
| |
| newLain.friends.push('小鸟游六花') |
| console.log(lain) |
# 数值表示
| const n1 = 100 |
| |
| const n2 = 0b100 |
| |
| const n3 = 0o100 |
| |
| const n4 = 0x100 |
| |
| console.log(n1, n2, n3, n4) |
# 连接符
| const n5 = 10_000_000 |
| console.log(n5) |
| |
| const n6 = 10_0.50 |
| console.log(n6) |
# Symbol
- ES2019 (ES10) 中,Symbol 还有一个描述符 (description)
| const s1 = Symbol('s1') |
| const s2 = Symbol('s2') |
| console.log(s1 === s2) |
| console.log(s1.description) |
| |
| const lain = Symbol('lain') |
| const saber = Symbol('saber') |
| |
| const characters = { |
| [lain]: 'lain' |
| } |
| |
| console.log(characters) |
| |
| |
| characters[saber] = 'saber' |
| |
| console.log(characters) |
| |
| |
| const Neko = Symbol('Neko') |
| Object.defineProperty(characters, Neko, { |
| enumerable: true, |
| configurable: true, |
| writable: true, |
| value: 'Neko' |
| }) |
| console.log(characters) |
| |
| |
| console.log(characters[lain], characters[saber], characters[Neko]) |
| |
| |
| console.log(Object.keys(characters)) |
| console.log(Object.getOwnPropertyNames(characters)) |
| |
| |
| const skeys = Object.getOwnPropertySymbols(characters) |
| console.log(skeys) |
| |
| |
| for (const skey of skeys) { |
| console.log(skey) |
| console.log(characters[skey]) |
| } |
| |
| |
| const foo = Symbol('foo') |
| |
| characters[foo] = function () { |
| console.log('foo') |
| } |
| characters[foo]() |
| |
| const s1 = Symbol.for('Hello World') |
| const s2 = Symbol.for('Hello World') |
| console.log(s1 === s2) |
| |
| |
| const key = Symbol.keyFor(s1) |
| console.log(key) |
| |
| |
| const s3 = Symbol.for(key) |
| console.log(s3 === s1) |