zoukankan      html  css  js  c++  java
  • ES6 new syntax of let and const (one)

    variable declarations : let, const,and block scope

    why we redefine the way about declarations?

    function scope

    var price = 10;  //global declaration
    
    function showPrice(){
    	var price = 12; //local declaration using var
    	console.log(price); //12
    }
    showPrice();
    
    console.log(price);  //10
    

    The next we can use IIFE to check var variable;

    var price = 10;
    (function(){
    	var price = 12;
    	console.log(price); //12
    })();
    console.log(price);  //10
    

    IIFE: Immediately Invoked Function Expression,意为立即调用的函数表达式,也就是说,声明函数的同时立即调用这个函数。

    How to change the scope of var variable?

    var price = 10;
    if(price){
    	price = 12;
    	console.log(price); 
    }
    console.log(price);
    

    The examples demonstrates _ of var.

    The example make me confusing.

    var price = 10;
    
    function showPrice(){
    	var price = 12;
    	console.log('price',price);
    }
    
    showPrice();
    console.log('showPrice',showPrice());
    console.log(price);
    

    the resule is

    price 12

    price 12

    showPrice undefined

    10

    why have two price?

    we add block scope like let and const to solve the problems.

    what is block scope?

    block scoping means that new scope is created between a pair of { }.

    let nbr = 12;
    {
    	let nbr = 40;
    }
    console.log(nbr)
    

    1.var is bound to function scope
    2.let and const are block scope

    How to install preview about markdown in sublime?

    1.we can use package install to install our plugins

    .first use keyboard open the package istall

    command+shift+p

    IIFE

    var price = 12;
    if(price){
        var price = 10;
        console.log(price);
    }
    console.log(price);
    

    IIFE tell us that the var declarations are bound to the function scope and does not create block scope.

    var is bound to function scope, let and const are block scope.

    let value = 42;
    {
        let value = 1000;
    }
    console.log(value);
    

    let varible only read in the block scope.

    const

    const declarations is a immutable varible.

    const value  = 50;
    console.log(value);
    
    value = 1000;
    
    const value  = 50;
    console.log(value);
    
    let value = 1000;
    

    varible hoisting

    console.log(host);
    var host = 89;
    

    In my heart,it may be console 89;Actualy it is undefiend;
    beacuse it is become next code in our browser

    var host;
    console.log(host);
    var host = 89;
    

    If you use let to declate a varible,it is err:

    console.log(host);
    let host =100;
    

    I'm confusing!

    TDZ(Temporal Dead Zone)

    You are accessing a varible that's been declared but not yet initialized.

    let data = true;
    if(true){
        console.log(data);
        let data;
    } 
    console.log(data);
    

    It's print the value

    undefiend
    true;

    But in the book ,author told me that print

    ReferenceError
    true

    let data = true;
    if(true){ //Enter new scope,TDZ starts
        //uninitialized bindling for data is created
        console.log(data); //ReferenceError
        let data;//TDZ ends,'data' is initialized with undefined
    }
    console.log(data); //true
    

    TDZs helps us ensure that a variable in runtime always have correct value.

    if(ture){
        console.log(typeof anUndeclaredVariable);
        console.log(typeof nrandom);
    
        let random;
    }
    

    It is a good practice to always make varible declarations at the top of your scope.

    This check is also useful for conditionally creating global varibles using var.
    You can check if a global variable exsits by doing something like this:

    if(typeof globalVariable === 'undefined'){
        var globalVariable = {...};
    }
    

    const in object

    You can add a property to object of const declaration but you cannot assign
    a different value to object.

    const obj = {};
    obj.key = 42;
    console.log(obj.key);
    
    obj = {};
    

    sure,if you really want to you could make the value itself immutable by freezing it.

    const obj = Object.freeze({});
    obj.key = 42;
    console.log(obj);
    

    Object.freeze() is shallow.

  • 相关阅读:
    将vs2010换成vs2012的主题
    写了个油猴脚本
    Myeclipse10下的access数据库配置
    点击按钮触发声音(xaml实现)
    自定义的可拖动窗体
    在博客添加时钟
    定时器写的闪光字
    C语言I博客作业02
    c语言|博客作业02
    关于软件工程的一些疑问
  • 原文地址:https://www.cnblogs.com/InnerPeace-Hecdi/p/8834883.html
Copyright © 2011-2022 走看看