function foo(x, y) { //这里面就不多严谨0在函数里面false
y = y || "word";
console.log(x, y)
}
foo("hello", 0)
function foo1 (x, y = "word") { //函数的默认值ES6的一些用法。参数的默认值一般需要放在后面
console.log(x, y) // 函数内部不能重复声明参数
}
foo1("hello", 0)
function foo2({x, y =5}) {
console.log(x, y)
}
foo2({x:2, y:10}) //解构赋值需要一一匹配
function ajax(url, {
body = "",
method = "GET",
headers = {}
} = {}){
console.log(method)
}
ajax("http://www.baidu.com")
function foo3(x, y = 2, z = 3){
console.log(x, y)
}
console.log(foo3.length) //length能够获取到函数没有默认值的个数
// 函数作用域
let x = 1;
function foo4(x, y = x){ //这里面的y指向的是函数里面x的作用域所以就是2
console.log(y)
}
foo4(2)
function foo5(y = x){ // 这里面是一个作用域,这里x没有值,就要按照作用域链找到全局变量x为1
let x = 2
console.log(y)
}
foo5()
//函数的name属性
console.log(new Function().name) //anonymous - 匿名的
foo5.bind({name:"yangang"})(2)
function foo6(a, b, c) {
console.log(a, b, c)
}
let arr = [1, 2, 3]
foo6(...arr)
let arr1 = [1, 2, 3]
let arr2 = [1, 2, 3]
let arr4 = Array.prototype.push.apply(arr1,arr2)
console.log(arr4) //这里面是6个元素
let arr3 = [...arr1,...arr2]
console.log(arr3)
//rest 参数 是 ... 逆操作
function sum(x, y, z){
let sum = 0
Array.prototype.forEach.call(arguments, function (item){
sum += item
})
return sum
}
console.log(sum(1, 2))
console.log(sum(1, 2, 3))
function sum1(...args){
console.log(args)
let sum = 0
args.forEach(function (item){
sum += item
})
return sum
}
console.log(sum1(1, 2))
console.log(sum1(1, 2, 3))
function sum2(x, ...args) {
console.log(x)
console.log(args)
}
let [a, ...b] = [1, 2, 3]
console.log(a)
console.log(...b)
一起设计吧