「JavaScript Understanding the Weird Parts」第二、三章筆記

總結

如題,記錄下課程中提到的相關關鍵字與額外補充的筆記

Section 2: Execution contexts and lexical environments

Syntax parser

What’s in an Interpretation?

Lexical environment

Lexical environment and function scope

那些年,我們一起錯過的 Javascript 系列:JS 的二三事

Understanding Closures in JavaScript

Execution context

Lexical environment and function scope

Creation phase

Execution phase

Global execution context

Global

undefined

name-value pair

Object (in JavaScript)

Invoke function, function invocation

scope

block scope

  {
    let a = 'apple';
    const c = 'cherry';
  }
  console.log(a); // ReferenceError
  console.log(c); // ReferenceError

  {
    var b = 'banana';
  }
  console.log(b); // 'banana'

scope chain

  function b() {
    console.log(myVar);
  }

  function a() {
    var myVar = 2;
    b();
  }

  var myVar = 1;
  a(); // 1
  function b(myVar) {
    console.log(myVar);
  }

  function a() {
    var myVar = 2;
    b(myVar);
  }

  var myVar = 1;
  a(); // 這樣才會印出2
  function a() {
    function b() {
      console.log(myVar);
    }

    var myVar = 2;
    b();
  }

  var myVar = 1;
  a(); // 這樣也會印出2

Single threaded

Synchronous

Asynchronous

Section 3 Types and Operators

Dynamic types

Primitive type

Operator

Operator precedence

Associativity

var a = 2, b = 3, c = 4;
a = b = c;
console.log(a); // 4
console.log(b); // 4
console.log(c); // 4
// = is right-to-left
0 || "0" && {}

Coercion

console.log(1 < 2 < 3) // true
console.log(3 < 2 < 1) // true
console.log(3 > 2 > 1) // false
// <, > is left-to-right

參考文件