Skip to content

参数默认值

函数参数是从左到右解析,如果没有默认值会被解析成 undefined

javascript
function test(x, y = 'world') {
  console.log('默认值', x, y)
  // 默认值 hello world
  // 默认值 hello wuchendi
}
test('hello')
test('hello', 'wuchendi')

参数默认赋值具体的数值

javascript
let x = 'test'
// function test2(x, y = x) {
// 	console.log("作用域", x, y);
// }
function test2(c, y = x) {
  console.log('作用域', x, y) // 作用域 test test
}
// test2();
test2('kill')

Rest 参数

javascript
function test3(...arg) {
  for (let v of arg) {
    console.log('rest', v)
    // rest 1
    // rest 2
    // rest 3
    // rest 4
    // rest a
  }
}
test3(1, 2, 3, 4, 'a')

扩展运算符

javascript
console.log(...[1, 2, 4]) // 1 2 4
console.log('a', ...[1, 2, 4]) // a 1 2 4

箭头函数

javascript
let arrow = (v) => v * 2
console.log('arrow', arrow(3)) // arrow 6

let arrow2 = () => 5
console.log('arrow2', arrow2()) // arrow 5

扩展

javascript
function tail(x) {
  console.log('tail', x) // tail 123
}
function fx(x) {
  return tail(x)
}
fx(123)

箭头函数和普通函数的区别

  1. 箭头函数和普通函数的样式不同,箭头函数语法更加简洁、清晰,箭头函数是=>定义函数,普通函数是function定义函数。
  2. 箭头函数会捕获其所在上下文的 this 值,作为自己的 this 值,定义的时候就确定并固定了。
  3. 箭头函数不能作为构造函数使用,也不能使用new关键字(因为箭头函数没有自己的this,它的this其实是继承了外层执行环境中的this,且this指向永远不会改变,作为构造函数其的this要是指向创建的新对象)。
  4. 箭头函数没有自己的arguments。在箭头函数中访问arguments实际上获得的是外层局部(函数)执行环境中的值。
  5. call、apply、bind 并不会影响其 this 的指向。
  6. 箭头函数没有原型prototype。
  7. 箭头函数不能当作 Generator 函数,不能使用 yield 关键字。

Layout Switch

Adjust the layout style of VitePress to adapt to different reading needs and screens.

Expand all
The sidebar and content area occupy the entire width of the screen.
Expand sidebar with adjustable values
Expand sidebar width and add a new slider for user to choose and customize their desired width of the maximum width of sidebar can go, but the content area width will remain the same.
Expand all with adjustable values
Expand sidebar width and add a new slider for user to choose and customize their desired width of the maximum width of sidebar can go, but the content area width will remain the same.
Original width
The original layout width of VitePress

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.

Spotlight

Highlight the line where the mouse is currently hovering in the content to optimize for users who may have reading and focusing difficulties.

ONOn
Turn on Spotlight.
OFFOff
Turn off Spotlight.