zoukankan      html  css  js  c++  java
  • ES6 基础(let const)

    ES6 基础(let const) 

    1、let的新特性

    let与var的区别
    1、 不能重复定义相同的变量名(let特点不能重复定义,使用let定义一次即可,多次使用let定义同一个变量 会报错),//报错  Identifier 'num' has already been declared
    2、let没有变量提升  有一个块级作用域  及{}就会产生一个块级作用域

       
    // let与var的区别 不能重复定义相同的变量名,//报错  Identifier 'num' has already been declared
        //let没有变量提升有一个块级作用域  及{}就会产生一个块级作用域
    
        // for(let i = 0;i<5;i++){
        //     var num = 10;
        //     let num = 5;
        //     console.log(num);
        // }
    
    
        // function fnn(name){
        //     let name = "李四";
        //     console.log(name);
        // }
        // fnn('张三');//报错,重复定义
        function fun1 (){
            console.log(num);
            var num = 1;//变量提升 undefined
        }
        fun1();
    
        function fun2 (){
            console.log(num);
            let num = 2;//报错  Cannot access 'num' before initialization
        }
        fun2();
      
    // for (var i=0; i<input.length;i++){
        //     (function (i) {
        //         input[i].onclick = function(){
        //             alert(i)
        //         }
        //     })(i)
        // }


        // let定义为let声明的变量仅仅在自己的块级作用域
            // 起作用,出了这个块级作用域就不起作用。
        for (let i = 0; i < input.length; i++) {

                    input[i].onclick = function () {
                        alert(i)
                    }

            }

      

    2、conts新特性

    const 声明常量

        1、不能重复声明常量名 
        2、声明后必须赋值 字符串,数字 布尔值等基本数据类型 (当常量第一数组,对象时,只要不改变给常量指向的地址,地址内存储的内容可以改变)
        3、常量特点不能修改const num1 ;
      4、没有变量提升
      5、具有块级作用域
     
    //const 声明常量
    
        //1、不能重复声明常量名 
        // var num =10;
        // const num = 15;
        // console.log(num)//Identifier 'num' has already been declared
    
        //声明后必须赋值 字符串,数字 布尔值等基本数据类型 常量特点不能修改const num1 ;
        // console.log(num1);//Missing initializer in const declaration
        // const person = {num:10,sex:"男"};//指向是一个地址,地址不能修改
        // console.log(person.num);
        // person.num = 40;//可以改变地址中的值
        // console.log(person.num);
    
        // person = {num:100};
        // console.log(person.num);//报错 
    
        // const arr = [1,2,3];
        // arr[1]=5;
        // console.log(arr[1]);//数组同理
    
        // function fun() {
        //     console.log(num);
        //     const num = 10;
        // }
        // fn()//报错  没有变量提升
        
        //具有块级作用域
        for(var j=0;j<5;j++){
            const num =10;
    
        }
        console.log(num);
    

      

     
  • 相关阅读:
    leetcode 18 4Sum
    leetcode 71 Simplify Path
    leetcode 10 Regular Expression Matching
    leetcode 30 Substring with Concatenation of All Words
    leetcode 355 Design Twitte
    leetcode LRU Cache
    leetcode 3Sum
    leetcode Letter Combinations of a Phone Number
    leetcode Remove Nth Node From End of List
    leetcode Valid Parentheses
  • 原文地址:https://www.cnblogs.com/wenaq/p/13588717.html
Copyright © 2011-2022 走看看