• ES6 let与const基础用法笔记


    640?wx_fmt=png

    ES2015(ES6)新增了两个JS关键字:let和const。主要是用来变量的声明。

    2、let 用法示例

    {

    let score=100;

    alert(score);

    }

    let 特性

    代码块内有效:let 定义的变量作用域:是靠近变量的代码块{}内有效

    示例:

    {

    let score=90;

    {

    let score=100;

    alert(score);//100

    }

    alert(score); //90

    }

    alert(score); // score is not defined

    同一个代码块变量只能声明一次

    示例:

    {

    let score=100;

    let score=80; //Identifier 'score' has already been declared

    alert(score);

    }

    let 变量必须先声明才可以使用,这点和var 是不同的

    示例:

    {

    alert(score); //score is not defined

    let score=100;

    }

    3、const 用法

    const:用于声明常量。变量一旦声明后,就不能修改。

    示例:

    {

    const PI=3.14;

    PI=3; //Assignment to constant variable.

    alert(PI);

    }

       

  • 相关阅读:
    单链表的逆转
    树的子结构和树的深度
    升级版爬楼梯问题
    蛇形数组
    正则表达式匹配
    构建乘积数组
    N皇后问题
    IOS计算文字高度
    Block的copy时机
    转:CocoaPods pod install/pod update更新慢的问题
  • 原文地址:https://www.cnblogs.com/hgmyz/p/12350955.html
Copyright © 2020-2023  润新知