/**
* 模板字符串
*/
// ES5
var retailPrice = 20
var wholesalePrice = 16
var type = 'retail'
var showTxt = ''
if (type === 'retail') {
showTxt += '您此次的购买单价是:' + retailPrice
} else {
showTxt += '您此次的批发价是:' + wholesalePrice
}
console.log(showTxt)
// ES6
/**
* @param: 定义一个 Tag 函数,然后用这个 Tag 函数来充当一个模板引擎
* @desc:
* - strings 返回模板客串中被所有变量分隔开的字符串集合
* - ype 参数对应模板字符串中的第一个变量,Tag 函数可以有多个 type 类似的参数
*/
// 参数:
function Price (strings, type) {
let s1 = strings[0]
const retailPrice = 20
const wholeSalePrice = 16
let showTxt = (type === 'retail') ? `购买单价是:${retailPrice}` : `购买单价是:${wholeSalePrice}`
return `${s1}${showTxt}`
}
console.log(Price`您此次的${'retail'}`)
拓展:
{
let [first, , third] = ['a', 'b', 'c', 'd']
console.log(first, third) // a c
}
{
let [first, , third] = 'abcd'
console.log(first, third) // a c
}
{
let { toString: a } = 123
let { toString: b } = false
console.log(a, b) // ƒ toString() { [native code] } ƒ toString() { [native code] }
}
{
let [first, , third] = new Set(['a', 'b', 'c', 'd'])
console.log(first, third) // a c
}
{
let [first, , third] = new Map([[1, 'a'], [2, 'b'], [3, 'c'], [4, 'd']])
console.log(first, third) // (2)[1, "a"] (2)[3, "c"]
}
{
let [first = 'A', , third, ...last] = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] // 为防undefined可赋初值
console.log(first, third, last) // a c (4)["d", "e", "f", "g"]
}
{
let obj = { A: 'a', B: 'b', C: 'c', D: 'd', E: 'e', F: 'f' }
let _obj = {
A: 'a',
B: {
C: {
D: 'd',
E: [1, 2, 3, 4, 5]
}
},
F: 'f'
}
// 数组解构
{
// 实体(key+value)
let [[firstK, firstV], , [thirdK, thirdV], ...last] = Object.entries(obj)
console.log(firstK, firstV, thirdK, thirdV, last) // A a C c (3)[Array(2), Array(2), Array(2)]
}
{
// Key
let [firstK, , thirdK, ...lastK] = Object.keys(obj)
console.log(firstK, thirdK, lastK) // A C (3)["D", "E", "F"]
}
{
// Value
let [firstV, , thirdV, ...lastV] = Object.values(obj)
console.log(firstV, thirdV, lastV) // a c (3)["d", "e", "f"]
}
// 对象解构
{
let { A, C, ...last } = obj
console.log(A, C, last) // a c {B: "b", D: "d", E: "e", F: "f"}
}
// 对象数组混合解构
{
let {
A,
B: {
C,
C: {
E: [, , third, ..._last]
}
},
...last
} = _obj
console.log(A, C, third, _last, last) // a {D: "d", E: Array(5)} 3 (2)[4, 5] {F: "f"}
}
}
[[1, 2], [3, 4], [5, 6], ['a', 'b']].map(([a, b]) => a + b) // (4) [3, 7, 11, "ab"]
{
function move1 ({ x = 0, y = 0 } = {}) {
return [x, y]
}
move1({ x: 3, y: 8 }) // [3, 8]
move1({ x: 3 }) // [3, 0]
move1({}) // [0, 0]
move1() // [0, 0]
}
{
function move2 ({ x, y } = { x: 0, y: 0 }) {
return [x, y]
}
move2({ x: 3, y: 8 }) // [3, 8]
move2({ x: 3 }) // [3, undefined]
move2({}) // [undefined, undefined]
move2() // [0, 0]
}
// undefined就会触发函数参数的默认值
[1, undefined, 3].map((x = 'yes') => x) // [ 1, 'yes', 3 ]
{
let cvs = document.getElementById('cvs')
let ctx = cvs.getContext('2d')
let drawLine = ([x, y, ...args]) => {
ctx.moveTo(x, y)
for (let i = 0; i < args.length; i++) {
ctx.lineTo(args[i].x, args[i].y)
}
ctx.stroke()
}
let line = [20, 20, { x: 30, y: 70 }, { x: 100, y: 30 }, { x: 200, y: 150 }]
drawLine(line)
}
let x = 1
let y = 2
console.log([x, y] = [y, x]) // [2, 1]
const { SourceMapConsumer, SourceNode } = require("source-map")
注意:
拓展: