2018年12月

<exploring ES6> 读书笔记 - Core ES6 features

  1. From var to const/let;
  2. From IIFEs(Immediately-Invoked Function Expression) to blocks;
  3. From concatenating strings to template literals -> ``;
  4. From function expressions to arrow functions, array function没有this;
  5. Handling multiple return values
    --Multiple return values via arrays //const [, year, month, day] = [0, 2012, 2, 2]
    --Multiple return values via objects //const {foo, bar} = {'foo':'full', 'bar':3, 'name':'eric'}
  6. From for and forEach() to for-of;
  7. Handling parameter default values -> function foo(x=0, y=0) {};
  8. From arguments to rest parameters (变长参数) foo(...args);
  9. 通过spread operator (...) 去把Array 变成非数组序列;
  10. From function expressions in object literals to method definitions;

    var obj = {

     foo: function () {
         ···
     },
     bar: function () {
         this.foo();
     }, // trailing comma is legal in ES5

    }

to

const obj = {
    foo() {
        ···
    },
    bar() {
        this.foo();
    },
}
  1. 使用 class 和 extend 定义类和继承关系

    class Employee extends Person {}

  2. From custom error constructors to subclasses of Error

    class MyError extends Error {}

  3. From objects to Maps;
  4. String 和 Array 新方法;

<exploring ES6> 读书笔记

About ECMAScript 6

  1. ECMA = European Computer Manufacturers Association, 是一个致力于信息和通讯标准化的组织. 更多信息
  2. ES6 = ECMAScript 6 是一个脚本语言规范. 最早是用来规范JavaScript的, 现如今JavaScript也是这个规范最好的实现.其它实现比如 JScript 和 ActionScript. 更多信息,比如ECMAScript 2015, ECMAScript 2016, ECMAScript 2017, ECMAScript 2018, 参见 wiki
  3. ES6 compatibility table
  4. ECMAScript 由TC39 (Ecma Technical Committee 39)委员会制定,它主要由大的浏览器厂商组成;
  5. ES6 亦 ECMAScript 2015 是多年后憋的一个大的release, 之后的ECMAScript 2016,等每年都会发布一些新的小更新;
  6. JavaScript是Oracle(从Sun继承而来)的一个商标,所以 JavaScript的官方名称是ECMAScript.
  7. 围绕 JavaScript 有: 1) JavaScript规范,即 ECMAScript 2015,2016,...; 2)JavaScript的Engine, 即规范实现者, 如V8; 3)JavaScript的使用者, 即应用开发人员;
  8. ES6是ES5的超集, 没有移除任何feature. 为什么? 难以控制的升级, 老旧代码,世界各地的各种不同的浏览器
  9. ES6的主要的features分类
    a. 更好的语法 如: Class, Module;
    b. 标准库的新功能, 如: Strings, Arrays, Promise, Map, Set;
    c. 新feature, 如: Generator, Proxy, WeakMap;
  10. Strict mode was introduced in ECMAScript 5 to clean up the language.

    'use strict';

The bodies of modules and classes are implicitly in strict mode in ECMAScript 6 – there is no need for the 'use strict' marker. Given that virtually all of our code will live in modules in the future, ECMAScript 6 effectively upgrades the whole language to strict mode.

关于 Node.js 的setImmediate, setTimeout, process.nextTick

要根据 Node.js 的event loop 来理解这些 setImmediate, setTimeout, process.nextTick

Node.js actually provides its own implementation of these methods. Timers integrate very closely with the system, and despite the fact that the API mirrors the browser API, there are some differences in implementation.

console.log("start of hello");

setImmediate(()=>{
    console.log("in setImmediate");
    setImmediate(()=>{
        console.log("in setImmediate's setImmediate"); //the 3rd loop
    });
});

setTimeout(()=>{
    console.log("in setTimeout");
    setImmediate(()=>{
        console.log("in setTimeout's setImmediate"); //2nd loop 
    });
}, 0);

process.nextTick(()=>{
    console.log("in nextTick"); //next event action
    process.nextTick(()=>{
        console.log("in nextTick's nextTick"); // next event action
    });
});

console.log("end of hello");

process.nextTick 是当前event action 处理完之后, 下一个会处理的action.

Node.js 文档

Node.js 使用event loop的并发模型 vs 其它语言的 多线程并发模型;
Node.js 的关键是 对于所有的非CPU 计算操作全部都使用异步的callback 模式;

基本语法

node [options] [V8 options] [script.js | -e "script" | -] [--] [arguments]
node inspect [script.js | -e "script" | <host>:<port>] …
node --v8-options

Node.js 对于 ES2015 Support
https://node.green/

Node.js 文档: https://nodejs.org/en/docs/

启用debug:
node --inspect hello.js
会打出如下:

chrome-devtools://devtools/remote/serve_file/@60cd6e859b9f557d2312f5bf532f6aec5f284980/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9229/6285270a-4795-40d7-992f-cfc452461144

然后打开9229端口:

http://localhost:9229/json/list

即便一开始没有启用 --inspect 参数, 启动之后, 还是可以通过发送 SIGUSR1 signal, 让它打开debuger (kill -l 查看全部signal)

Node.js is an event-based platform. This means that everything that happens in Node is the reaction to an event. A transaction passing through Node traverses a cascade of callbacks.

关于 Event Loop: link
https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop