1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <script>
- // function foo(a,b){
- // if(a && b){
- // console.log(a+b);
- // }else{
- // console.log("缺少参数");
- // }
- // }
- // foo(1)
- // ES6 新增默认值 如果参数有值 就用参数的值 如果没有值 就用默认值
- // function foo(a=0,b=0){
- // console.log(a+b);
- // }
- // foo(1,2)
- // function foo(a,b,c = 0){
- // console.log(a+b+c);
- // }
- // foo(1,2,3)
- // 函数length 属性 代表形参的个数 且没有默认值
- // console.log(foo.length);
- // function foo(){};
- // let foo = function(){};
- // ES6 新增箭头函数 语法更加简洁
- // let foo = () => {};
- // let foo = (a,b) => {
- // console.log(a+b);
- // }
- // foo(1,2);
- // 当箭头函数省略大括号时 会默认返回值 返回箭头后边的第一条语句;
- // let foo = (a,b) => a+b;
- // console.log(foo(1,2));
- // function foo(a,b){
- // return a+b;
- // }
- // console.log(foo(1,2));
- // function foo(...arg){
- // console.log(arg);
- // }
- // foo(1,2,3);
- // function foo(){
- // console.log(arguments[0]);
- // }
- // foo(1,2,3,4,5)
- // let foo = () => {
- // console.log(arguments);
- // }
- // let foo = (...arg) => {
- // console.log(arg);
- // }
- // foo(1,2,3,4,5);
- // let foo = () => {
- // console.log(this);
- // }
- // foo();
- let obj = {
- a:100,
- b:"hello",
- foo(){
- // console.log(this);
- let foo2 = () => {
- console.log(this);
- }
- foo2();
- function foo3(){
- console.log(this);
- }
- foo3();
- }
- }
- obj.foo();
- </script>
- </body>
- </html>
|