zoukankan      html  css  js  c++  java
  • es6 初级之---const 和 默认参数

    1. const 的定义:

    1.1 常量定义的时候要赋值,不赋值是会报错的:

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     6     <meta http-equiv="X-UA-Compatible" content="ie=edge">
     7     <title>Document</title>
     8     <script>
     9         // 常量: 不能被改变值的 量 叫常量
    10         // 1, 常量最开始定义的时候, 要给初始值
    11         // 2, 常量不能被重新赋值
    12         // 3, 常量的作用域是块级的
    13         // 4,个人感觉有点像php 中的define
    14         const a; //报错,没有初始化一个值
    15     </script>
    16 </head>
    17 <body>
    18 </body>
    19 </html>

    1.2 定义同时赋值

    1       const a = 10;
    2       console.log(a);

    1.3 常量定义之后,不能再次赋值

    1         const a = 10;
    2         a = 100; //报错,不能给常量重新赋值
    3         console.log(a);

    1.4 常量是块级作用域

    1         let a = 10;
    2         if (a) {
    3             const b = 100;
    4             console.log(b);
    5         }
    6         console.log(b); //报错,访问不到块级作用域中的常量

    1.5 对象常量不能改变:

    1         const user = {
    2             "name": "nihaome",
    3             age: 22
    4         };
    5         user = { //报错,常量不能被重新赋值
    6             "name": 'zhangsan'
    7         }
    8         console.log(user);

    1.6 对象常量的属性可以被改变:

    如果常量定义的值 是一个对象, 那么对象不能被修改,
    但是对象的属性值可以被修改
    1        const user = {
    2             "name": "nihaome",
    3             age: 22
    4         };
    5         user.name = 'zhangsan';
    6         console.log(user);

    2. 默认参数:

    2.1 默认参数之一:

    1        function show(a, b) {
    2             a = a || 10;
    3             b = b || 20;
    4             console.log(a, b);
    5         }
    6         show(1, 2);

    2.2 默认参数之二:

    1        function show(a, b) {
    2             a = a || 10;
    3             b = b || 20;
    4             console.log(a, b);
    5         }
    6         show();

    2.3 默认参数之三:

    1        function show(a, b) {
    2             a = a || 10;
    3             b = b || 20;
    4             console.log(a, b);
    5         }
    6         show(1);

    2.3 默认参数之四:

    1        function show(a, b) {
    2             a = a || 10;
    3             b = b || 20;
    4             console.log(a, b);
    5         }
    6         show(0, 0); //0会转成false

    默认参数:有传入参数值的时候,就使用传递的参数;没有传递参数,就使用默认参数

    3. 表达式:先判定,再赋值

    1         function show(a, b) {
    2             a = typeof a === 'undefined' ? 10 : a;
    3             b = typeof b === 'undefined' ? 20 : b;
    4             console.log(a, b);
    5         }
    6         show(undefined, 30);
    7         show();

    4. 默认参数

    1         function show( a = 10, b = 20, c = 30 ){
    2             console.log( a, b, c );
    3         }
    4         show();                     // 10, 20, 30, 没有传递参数就使用默认值
    5         show( 100 );                // 100, 20, 30, 没有传递参数就使用默认值
    6         show( 100, 200 );           // 100, 200, 30, 没有传递参数就使用默认值
    7         show( 100, 200, 300 );      // 100, 200, 30, 没有传递参数就使用默认值

    5. 默认参数 undefined

     1         function show(name, age = 22, sex) {
     2             console.log(name, age, sex);
     3         }
     4         //函数在没有传值得时候,默认为undefined
     5         show(); //undefined,22,undefined  使用name,age,sex的默认参数
     6 
     7         //函数显示的传递undefined,相当于没有传递参数,所以age用默认值22
     8         show(undefined, undefined, undefined); //undefined,22,undefined
     9 
    10         //函数传递null的时候,不会等于undefined,相当于传null值, 会把age的默认值覆盖
    11         show(undefined, null, undefined); //undefined, null, undefined

    6. 非严格模式下,参数会同步到arguments

     1        //在非严格模式下,参数的修改会同步到arguments
     2         function show(name, age) {
     3             console.log(name == arguments[0]); //true
     4             console.log(age == arguments[1]); //true
     5             name = 'zhangsan';
     6             age = 30;
     7             console.log(name == arguments[0]); //true
     8             console.log(age == arguments[1]); //true
     9         }
    10         show('hehheh', 22);

    7. 严格模式下,参数与arguments 不同步(严格模式,在代码前加入一行"use strict")

     1        "use strict";
     2 
     3         function show(name, age) {
     4             console.log(name == arguments[0]); //true
     5             console.log(age == arguments[1]); //true
     6             name = 'zhangsan';
     7             age = 30;
     8             console.log(name == arguments[0]); //false
     9             console.log(age == arguments[1]); //false
    10         }
    11         show('hehhe', 22);

    8.默认参数和arguments:默认参数和arguments 是有差异的

     1         function show(name, age = 22) {
     2             console.log(arguments.length); //1
     3             console.log(name === arguments[0]); //true
     4             console.log(age, arguments[1]); //22, undefined
     5             console.log(age === arguments[1]); //false
     6 
     7             name = 'zhangsan';
     8             age = 30;
     9             console.log(name, age, arguments[0], arguments[1]); //zhangsan, 30, nani, undefined
    10             console.log(name == arguments[0]); //false
    11             console.log(age == arguments[1]); //false
    12         }
    13         show('nani');            

    9. 函数作为默认参数:

     1        function getVal() {
     2             return 10;
     3         }
     4 
     5         function add(a, b = getVal()) {
     6             return a + b;
     7         }
     8 
     9         //相当于 a = 100, b = 200
    10         console.log(add(100, 200)); //300
    11         //相当于 a = 100, b没有传值, 采用getVal()的返回值10
    12         console.log(add(100)); //110

    http://www.cnblogs.com/huanying2015 博客随笔大多数文章均属原创,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利
  • 相关阅读:
    mvc
    拦截器
    使用HttpWebRequest和HtmlAgilityPack抓取网页(拒绝乱码,拒绝正则表达式)
    编译和解释的区别是什么?
    15 个最佳的 jQuery 表格插件
    编程小白必备——主流语言C语言知识点
    妹子找你修电脑,按照这几步操作,你就是黑客大佬!
    网络管理监视很重要!学编程的你知道哪些不错的网络监控工具?2020 最好的Linux网络监控工具分享给你
    为什么程序员要跳槽,钱并非第一位
    代码编写行为准则,编码是一个认真思考的过程,如何有效提高代码的可读性?
  • 原文地址:https://www.cnblogs.com/huanying2015/p/8302726.html
Copyright © 2011-2022 走看看