zoukankan      html  css  js  c++  java
  • xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

    JavaScript & Automatic Semicolon Insertion

    ECMA 262 真香警告⚠️

    https://www.ecma-international.org/ecma-262/6.0/index.html#sec-automatic-semicolon-insertion

    Certain ECMAScript statements (empty statement, let, const, import, and export declarations, variable statement, expression statement, debugger statement, continue statement, break statement, return statement, and throw statement) must be terminated with semicolons. Such semicolons may always appear explicitly in the source text. For convenience, however, such semicolons may be omitted from the source text in certain situations. These situations are described by saying that semicolons are automatically inserted into the source code token stream in those situations.

    https://www.ecma-international.org/ecma-262/5.1/#sec-7.9

    Certain ECMAScript statements (empty statement, variable statement, expression statement, do-while statement, continue statement, break statement, return statement, and throw statement) must be terminated with semicolons. Such semicolons may always appear explicitly in the source text. For convenience, however, such semicolons may be omitted from the source text in certain situations. These situations are described by saying that semicolons are automatically inserted into the source code token stream in those situations.

    wiki

    https://en.wikibooks.org/wiki/JavaScript/Automatic_semicolon_insertion

    1. 不要在 return 后换行,不要在一条语句中换行;
    2. 不要将开括号{ 放在单独的一行上

    i = 3;
    // 3
    if (i === 5)
      // assuming a semicolon here
      console.log(`if`)
    else
      console.log(`else`);
    // else
    
    if (i === 5)
      // assuming a semicolon here
      console.log(`if`);
    else
      console.log(`else`);
    // else
    
    i = 5;
    // 5
    if (i === 5)
      // assuming a semicolon here
      console.log(`if`)
    else
      console.log(`else`);
    //  if
    
    if (i === 5)
      // assuming a semicolon here
      console.log(`if`);
    else
      console.log(`else`);
    //  if
    
    
    

    分步 OK

    // "use strict";
    let type = "Literal"
    let name = 5
    let node = {
      type: "Identifier",
      name: "foo"
    }
    
    
    // 隔开 OK
    ({ type, name } = node)
    console.log(type)
    console.log(name)
    // Identifier
    // foo
    
    

    同步 bug

    "use strict";
    
    /**
     *
     * @author xgqfrms
     * @license MIT
     * @copyright xgqfrms
     * @created 2020-07-29
     * @modified
     *
     * @description Automatic Semicolon Insertion
     * @difficulty Easy Medium Hard
     * @complexity O(n)
     * @augments
     * @example
     * @link
     * @solutions
     *
     */
    
    const log = console.log;
    
    
    // IIFE
    (() => {
      "use strict";
      let type = "Literal"
      let name = 5
      let node = {
        type: "Identifier",
        name: "foo"
      }
    
      ({ type, name } = node)
      // Uncaught ReferenceError: Cannot access 'node' before initialization
      console.log(type)
      console.log(name)
    })();
    

    refs

    https://www.zhihu.com/question/270095202

    https://www.zhihu.com/question/270095202/answer/1368566113


    Flag Counter

    ©xgqfrms 2012-2020

    www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


  • 相关阅读:
    路由器漏洞调试的一些技巧
    路由器漏洞挖掘利用的一些基础知识
    windows 利用环境变量%PATH%中目录可写提权

    python super原理,不是指父类
    regexp盲注的一些改进
    阿里规范
    阿里规范
    工具类
    Timer 使用 (一)
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/13396025.html
Copyright © 2011-2022 走看看