JS函数和异常

函数

function 函数名(参数列表) {
	函数体;
	return 返回值;
}
function add(x,y){
	return x+y;
}
console.log(add(3,5));

函数表达式

使用表达式来定义函数,表达式中的函数名可以省略,如果这个函数名不省略,也只能用在此函数内部。

//匿名函数

const add = function(x,y){
    return x+y;
}
console.log(add(4,5));


//有名字的函数表达式
const sub = function fn(x,y){
    return x - y;
}

console.log(sub(5, 3));

//console.log(sub2(3, 2)); fn只能用在函数内部
// 有名字的函数表达式
const sum = function _sum(n) {
    if (n===1) return n;
    return n + _sum(--n); // _sum只能内部使用
   }
   console.log(sum(4));

高阶函数

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

完成一个计数器counter

const counter = function (){
    let c = 0;
    return function(){
        return ++c;
    }
}
const c = counter()
console.log(c());
console.log(c());
console.log(c());

练习

完成一个map函数:可以对某一个数组的元素进行某种处理

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]));

箭头函数

箭头函数参数

  • 如果一个函数没有参数,使用()
  • 如果只有一个参数,参数列表可以省略小括号
  • 多个参数不能省略小括号,且使用逗号间隔

箭头函数返回值

如果函数体部分有多行,就需要使用{},如果有返回值使用return

如果只有1行语句,可以同时省略大括号和return

只要有return语句,就不能省略大括号。 console.log(map([1,2,3,4], x => {return ++x})) ,有return必须有大括号。

console.log(map((x) => {return x * 2}, [1,2,3,4]));
console.log(map(x => {return x * 2}, [1,2,3,4]));
console.log(map(x => x * 2, [1,2,3,4]));

函数参数

普通参数

一个参数占一个位置,支持默认参数

const add = (x,y) => x + y
console.log(add(4, 5))


// 缺省值
const add1 = (x, y=5) => x + y
console.log(add1(4, 7))	//9
console.log(add1(4))	//11

如下函数

const add2 = (x=6,y) => x+y
console.log(add2());
console.log(add2(y=1));
console.log(add2(y=2,z=3))

上面add2的调用结果分别为

NaN、NaN、5

JS中并没有Python中的关键字传参

JS只是做参数位置的对应

JS并不限制默认参数的位置

add2()相当于add(6, undefined)

add2(1)相当于add(1, undefined)

add2(y=2,z=3)相当于add2(2,3),因为JS没有关键字传参,但是它的赋值表达式有值,y=2就是2,z=3就是3

建议,默认参数写到后面。

可变参数

JS使用…表示可变参数(Python用*收集多个参数)

const sum = (...args) => {
    let result = 0;
    for (let x of args){
        result += x;
    }
    return result;
};

console.log(sum(3,6,9));

参数解构

和python类似,js提供了参数解构,依然使用了…符号来解构

const add = (x,y) => {console.log(x,y); return x+y;}
console.log(add(...[100,200]));
console.log(add(...[100,200,300,400]));
console.log(add(...[100]))

js支持参数解构,不需要解构后的值个数和参数个数相对应

函数返回值

python 中可以使用 return 1,2 返回多值,本质上也是一个值,就是一个元组。Js中呢?

const add = (x,y) => {return x,y}
console.log(add(4,100)); // 返回什么? 100

表达式的值

类C语言,都有一个概念—表达式的值

赋值表达式的值,等号右边的值

逗号表达式的值:类C语言,都支持逗号表达式,逗号表达式的值,就是最后一个表达式的值

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

b = (123, true, z = 'test')
console.log(b); //test
function c() {
    return x = 5, y = 6, true, 'ok';
}
console.log(c()); //ok

js的函数返回值是单值

作用域

// 函数中变量的作用域
function test(){
    a = 100;
    var b = 200;
    let c = 300;
}
// 先要运行test函数
test()
console.log(a);
console.log(b); // 不可见
console.log(c); // 不可见
// 块作用域中变量
if (1){
    a = 100;
    var b = 200;
    let c = 300;
}
console.log(a);
console.log(b);
console.log(c); // 不可见

function是函数的定义,是一个独立的作用域,其中定义的变量在函数外不可见。

var a = 100 可以提升声明,但不可以突破函数作用域。

a = 100 隐式声明不能提升声明,在“严格模式”下会出错,但是可以把变量隐式声明为全局变量。建议少用。

let a = 100 不能提升声明,而且不能突破任何的块作用域。推荐使用。

严格模式:使用"use strict";,这条语句放到函数的首行,或者js脚本首行

异常

抛出异常

js的异常语法和java相同,使用throw关键字抛出

使用throw关键字可以抛出任意对象的异常,但是请使用Error类型

throw new Error('new error');
throw new ReferenceError('Ref Error');
throw 1;
throw 'not ok';
throw [1,2,3];
throw {'a':1};
throw () => {}; // 函数

捕获异常

try…catch 语句捕获异常。

try…catch…finally 语句捕获异常,finally保证最终一定执行。

注意这里的catch不支持类型,也就是说至多一个catch语句。可以在catch的语句块内,自行处理异常。

try {
    //throw new Error('new error');
    //throw new ReferenceError('Ref Error');
    //throw 1;
    //throw new Number(100);
    // throw 'not ok';
    // throw [1,2,3];
    // throw {'a':1};
    throw () => {}; // 函数
} catch (error) {
    console.log(error);
    console.log(typeof(error));
    console.log(error.constructor.name);
    console.log(error.message);
} finally {
    console.log('===end===')
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值