Skip to content

Instantly share code, notes, and snippets.

@xgqfrms-GitHub
Last active July 3, 2020 17:05
Show Gist options
  • Save xgqfrms-GitHub/19479fdadbbc05356aceaf56073bec6d to your computer and use it in GitHub Desktop.
Save xgqfrms-GitHub/19479fdadbbc05356aceaf56073bec6d to your computer and use it in GitHub Desktop.
ecma-262 6.0 class

How To Reading ECMA-262

http://www.ecma-international.org/ecma-262/6.0/#sec-class-definitions

14.5 Class Definitions

Syntax

ClassDeclaration[Yield, Default] :
    class BindingIdentifier[?Yield] ClassTail[?Yield]
    [+Default] class ClassTail[?Yield]
    
ClassExpression[Yield] :
    class BindingIdentifier[?Yield]opt ClassTail[?Yield]
    
ClassTail[Yield] :
    ClassHeritage[?Yield]opt { ClassBody[?Yield]opt }
    
ClassHeritage[Yield] :
    extends LeftHandSideExpression[?Yield]
    
ClassBody[Yield] :
    ClassElementList[?Yield]
    
ClassElementList[Yield] :
    ClassElement[?Yield]
    ClassElementList[?Yield] ClassElement[?Yield]
    
ClassElement[Yield] :
    MethodDefinition[?Yield]
    static MethodDefinition[?Yield]
    ;

NOTE A ClassBody is always strict code.

blogs

https://chadaustin.me/2012/12/javascript-new-operator-ecma-262/

https://stackoverflow.com/questions/2738736/confused-with-ecmascript-language-specification-function-calls-section

http://stackoverflow.com/questions/761905/how-will-ecma-262-ecmascript-5-help-you

https://blog.appdynamics.com/engineering/7-ways-es2015-can-improve-your-javascript-programing/

https://leanpub.com/understandinges6/read

https://lightrains.com/blogs/es6#classes

@xgqfrms-GitHub
Copy link
Author

xgqfrms-GitHub commented May 10, 2017

es6-in-depth-let-and-const

http://blog.herobs.cn/es6-in-depth-let-and-const/

letvar 究竟有哪些具体的差异?

  1. let 变量是块级作用域。其作用域只在块中,而不再是整个函数。

  2. for (let x...) 循环将会给每次循环创建一个新的 x 绑定。

  3. 任何尝试在 let 变量声明前使用变量将会抛出异常。(no hoisting ?)

  4. 使用 let 重复声明变量将会引起 Syntax Error(语法错误)。

  5. let 在严格模式是一个保留关键词,在非严格模式中,为了能够后向兼容,你任然可以使用 let 作为变量名、函数名和参数名.
    var let = 'aHa';

Closures

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Closures

闭包是指这样的作用域,它包含有一个函数,这个函数可以调用被这个作用域所封闭的变量、函数或者闭包等内容。
通常我们通过闭包所对应的函数来获得对闭包的访问。

https://www.w3schools.com/js/js_function_closures.asp

let messages = ["Meow!", "I'm a talking cat!", "Callbacks are fun!"];
    for (var i = 0; i < messages.length; i++) {
        console.log(`inner i = ${i}`);
        setTimeout(function () {
            console.log(`messages[i] = ${messages[i]}`);
        }, 0);
    }
    console.log(`outer i = ${i}`);


let messages = ["Meow!", "I'm a talking cat!", "Callbacks are fun!"];
    for (let i = 0; i < messages.length; i++) {
        console.log(`inner i = ${i}`);
        setTimeout(function () {
            console.log(`messages[i] = ${messages[i]}`);
        }, 0);
    }
    console.log(`outer i = ${i}`);

let i

const

  1. const 声明的变量和 let 是一样的,但是你不能给他们赋值,否则将会抛出语法错误

  2. 在声明 const 变量时, 给定初始值。

const

@xgqfrms-GitHub
Copy link
Author

JavaScript 变量提升(Hoisting)

http://www.dongcoder.com/detail-270846.html

https://zonxin.github.io/post/2015/10/javascript-hoisting

http://www.jianshu.com/p/159b95d3e533

http://www.bbsmax.com/R/B0zqv2QJvL/

Closures

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Closures

闭包是指这样的作用域,它包含有一个函数,这个函数可以调用被这个作用域所封闭的变量、函数或者闭包等内容。
通常我们通过闭包所对应的函数来获得对闭包的访问。

// global  var
var counter = 0;

function add() {
    counter += 1;
    console.log(counter);
}

add();
// 1
add();
// 2
add();
// 3


// local var
function add() {
    var counter = 0;
    // overwrite
    counter += 1;
    console.log(counter);
}

add();
// 1
add();
// 1
add();
// 1

var local-vs-global

@xgqfrms-GitHub
Copy link
Author

闭包

function init() {
  let name = "Mozilla"; 
  // name 是一个被init创建的局部变量
  function displayName() { 
  // displayName() 是一个内部函数,
      alert(name); 
      //  一个闭包使用在父函数中声明的变量
  } 
  displayName();
}
init(); 

let-var already been declared

add()(); === var add = add(); add();

function add() {
    var counter = 0;
    return function () {
        console.log(counter);
        return counter += 1;
    }
}

add()();
// var add = add(); add();

@xgqfrms-GitHub
Copy link
Author

es6 let & emoji

https://github.com/xgqfrms-GitHub/Native-JavaScript/blob/master/es6-emoji.html

https://jsfiddle.net/sybn4h33/3/

You can't comment at this time — your comment contains unicode characters above 0xffff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment