<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//es5
// function fun1(a,b){
// if(typeof b === 'undefined'){
// b = 'world'
// }
// console.log(a,b)
// }
// fun1('hello','') //hello
// fun1('hello') //hello world
// fun1('hello','world')//hello world
// 1. &&
// 1.1两边条件都为true时,结果才为true;
// 1.2如果有一个为false,结果就为false;
// 1.3当第一个条件为false时,就不再判断后面的条件
// 注意:当数值参与逻辑与运算时,结果为true,那么会返回的会是第二个为真的值;如果结果为false,返回的会是第一个为假的值。
// 2. ||
// 2.1只要有一个条件为true时,结果就为true;
// 2.2当两个条件都为false时,结果才为false;
// 2.3当一个条件为true时,后面的条件不再判断
// 注意:当数值参与逻辑或运算时,结果为true,会返回第一个为真的值;如果结果为false,会返回第二个为假的值
// function fun1(a, b) {
// b = b || 'world'
// console.log(a, b)
// }
// fun1('hello', '')//hello world
// fun1('hello')//hello world
// fun1('hello', 'world')//hello world
// //es6
// //给形参设置默认值的方式
// function fun1(a = 'hello', b = 'world'){
// console.log(a,b)
// }
// fun1()//hello world
// fun1('hello')//hello world
// fun1('hello', 'world')//hello world
// //剩余参数(rest参数),形式为(...变量名)
// function fun(x,y){
// console.log(x,y)
// console.log(arguments) //arguments是所有的实参值
// }
// fun(1,2,3,4,5,6,7) //1 2 Arguments(7) [1, 2, 3, 4, 5, 6, 7, callee: ƒ, Symbol(Symbol.iterator): ƒ]
// function fun(x,y,...arg){
// console.log(x,y,arg)
// }
// fun(1,2,3,4,5,6,7) //1 2 [3, 4, 5, 6, 7]
// function fun(...arg){
// console.log(arg)
// }
// fun(1,2,3,4,5,6,7) //[1, 2, 3, 4, 5, 6, 7]
</script>
</body>
</html>
函数的新特性
最新推荐文章于 2024-07-06 16:47:02 发布