函数的参数

//函数的参数

// function foo(x, y) {
// y = y || 'world'
// console.log(x, y)
// }
// // foo('hello', 'imooc')
// foo('hello', 0) 有缺陷 不能输出 hello 0


// function foo(x, y = 'world') {
// console.log(x, y)
// }

// foo('hello', 0 )

// 注意参数的默认值 要放在参数的最后面

// function foo(x, y, z = 5) {
// console.log(x, y, z)
// }
// foo(1,3) // => 1, 3, 5

//2与解构赋值的结合

// function foo({x, y = 5}) {
// console.log(x, y)
// }
// // foo({}) => undefined 5

// foo({
// x: 1
// }) // => 1 5

// foo({
// x: 1,
// y: 2
// }) // => 1 2

// function ajax(url, {
// body = "",
// method = 'GET',
// headers = {}
// } = {}) {
// console.log(method)
// }

// ajax('http://wwww.imooc.com', {
// method: 'POST'
// })

//3 length
// function foo(x, y, z = 1) {
// console.log(x, y, z)
// }
// console.log(foo.length) // => 返回没有指定默认值的参数个数 2

// 4 函数的作用域

// let x = 1
// function foo(x, y = x) {
// console.log(y)
// }

//foo(2)

// let x = 1
// function foo(y = x) {
// let x = 2
// console.log(y) // => 1
// }

//foo() 不传值 当前作用域为空 顺着作用域链 往外找 找到全局变量1 所以值为1

// function foo(y = x) {
// let x = 2
// console.log(y)
// }

// foo() // => 报错 x is not defined

// 5 函数的name属性
// function foo() {}
// console.log(foo.nme) // undefined
// console.log((new Function).name) // anonymous [əˈnɑːnɪməs] 匿名的

// function foo(x, y) {
// console.log(this, x, y) // 希望this指向谁 bind里就传谁
// }

// foo.bind({name: 'hky'})(1, 2)

// console.log(foo.bind({}).name) // 当函数用bind 会自带一个单词 bound foo 为当前函数的名字

// console.log((function(){}).bind({}).name) // 只有一个bound

posted @ 2021-02-19 21:09  贺可英  阅读(39)  评论(0)    收藏  举报