作用域
情况一
var x = 1
function f(x, y = x) {
console.log(y)
}
f(2) // 2
情况二
let x = 1
function f(y = x) {
let x = 2
console.log(y)
}
f() // 1
情况三
情况四
情况五
情况六
Last updated
var x = 1
function f(x, y = x) {
console.log(y)
}
f(2) // 2
let x = 1
function f(y = x) {
let x = 2
console.log(y)
}
f() // 1
Last updated
function f(y = x) {
let x = 2
console.log(y)
}
f() // ReferenceError: x is not defined
var x = 1
function foo(x = x) {
// ...
}
foo() // ReferenceError: x is not defined
let foo = 'outer'
function bar(func = () => foo) {
let foo = 'inner'
console.log(func)
}
bar() // outer
function bar(func = () => foo) {
let foo = 'inner'
console.log(func())
}
bar() // ReferenceError: foo is not defined
var x = 1
function foo(x, y = function() { x = 2 }) {
var x = 3
y()
console.log(x)
}
foo() // 3
x // 1
var x = 1;
function foo(x, y = function() { x = 2; }) {
x = 3;
y();
console.log(x);
}
foo() // 2
x // 1