NodeJS 函数与作用域

三、函数与作用域

3.1 函数

function 函数名(函数参数) {
    函数体;
    return 返回值;
}
function add(x,y){
    return x+y
}

ret = add(4,5)
console.log(ret)

3.2 函数表达式

函数、匿名函数、函数表达式本质上都是函数对象,只不过函数有自己的标识名字,而匿名函数需要借助其它标识,他们的区别在于,函数会声明提升,而匿名函数不行

  • 函数:有自己的标识名字可以在任意位置调用
  • 匿名函数:必须在声明的行后面才能调用
// 匿名函数
const add = function(x,y) {
    return x+y;
}
console.log(add(4, 5)) // 9


// 有名字的函数表达式
// console.log(sum(5))  // 此处调用会报错,而函数不会
const sum = function _sum(n) {
    if (n==1) return n;
    return n+_sum(--n)
}
console.log(sum(5)) // 15

3.3 高阶函数

函数作为参数或者返回一个函数的函数为高阶函数

下面的map函数,可以实现对某一个数组元素进行某种处理,例如:将[1,2,3,4]转为[2,3,4,5]

const map = function(fn, arr) {
    let newarr = [];
    for (let i in arr) {
        newarr[i] = fn(arr[i]);
    }
    return newarr
}

console.log(map(function(x) {return x+1}, [1,2,3,4]))

使用生成器的办法改写map函数

var map = function* (fn, arr) {
    for (i in arr) {
        yield fn(arr[i]);
    }
}

let newarr = map(function(x) {return x+1}, [1,2,3,4])
console.log(...newarr)

3.4 箭头函数

箭头函数就是匿名函数,他是一中更加精简的格式

  • 如果一个函数没有参数,则使用()=> {console.log(x);return x+1}
  • 如果只有一个参数,那么参数列表可以省略小括号:x=> {console.log(x);return x+1}
  • 如果只有一行,则可以省略大括号和return: x=>x+1
  • 有return必须有{},两个是成对出现的
var map = function* (fn, arr) {
    for (i in arr) {
        yield fn(arr[i]);
    }
}

// 下面三行等价
let newarr1 = map(function(x) {return x+1}, [1,2,3,4])
let newarr2 = map((x) => {return x+1}, [1,2,3,4])
let newarr3 = map(x => x+1, [1,2,3,4])

3.5 函数参数

3.5.1 普通参数

一个参数占一个位置,支持默认参数,函数只做参数位置的对应,对参数的个数不限制,多穿少传都不会报错

const foo = function (x,y) {
    console.log(x,y);
}

foo(100)
foo(100,200,300)
foo(y=100,x=200)
console.log("-----------------------------")
const bar = function (x=100,y) {
    console.log(x,y);
}
bar(400)
bar(400, 100)
bar('a', 'b', 'c')

输出结果:
100 undefined
100 200
100 200
-----------------------------
400 undefined
400 100
a b

3.5.2 可变参数

js 使用 ... 表示可变参数

  • es6 之前使用 arguments对象将所有参数组装程一个键值对字典保存
  • es6 开始,不推荐使用arguments
const sum = function(...args) {
    console.log(arguments)

    let s = 0
    for (let i in args) {
        s +=args[i]
    }
    return s
}

console.log(sum(1,2,3,4,5))

输出结果:
[Arguments] { '0': 1, '1': 2, '2': 3, '3': 4, '4': 5 }
15

3.5.3 参数结构

使用 ... 解构

const add = (x, y) => {console.log(x, y)}

add(...[100])
add(...[100,200])
add(...[100,200,300])

输出结果:
100 undefined
100 200
100 200

3.6 函数返回值

python中可以通过 return 1,2 返回一个二元组,但是js只能返回逗号表达式中最后一个值

a = (x=5, y=6, true)
console.log(a)

b = (123, 'abc', z='test')
console.log(b)

function c() {
    return x=5,y=6, true, 'ok'
}
console.log(c())

输出结果:
true
test
ok

3.7 作用域

  • x=100: x可以突破块或函数作用域称为一个全局变量,在严格模式下会报错"use strict"
  • let:let声明的变量为局部变量,不能进行声明提升,推荐使用
  • var: var声明的变量可以突破块作用域,但是不能突破函数作用域
function test() {
    a = 100;
    var b = 200;
    let c = 300;
    {
        console.log( a)
        console.log( b)
        console.log( c)
    }
}

test()
console.log("------------")
console.log(a)
// console.log(b)  // 不可见
// console.log(c) //不可见

输出结果:
100
200
300
------------
100
if(1) {
    a = 100;
    var b = 200;
    let c = 300;
    {
        console.log( a)
        console.log( b)
        console.log( c)
    }
}
console.log("-------------")
console.log(a)
console.log(b)
// console.log(c) 不可见

posted on 2023-07-20 15:20  kntvops  阅读(66)  评论(0编辑  收藏  举报

导航