Skip to content

Instantly share code, notes, and snippets.

@0bie
Last active October 18, 2016 16:13
Show Gist options
  • Save 0bie/d4b56a61569e618dd047dc4e434d9f6b to your computer and use it in GitHub Desktop.
Save 0bie/d4b56a61569e618dd047dc4e434d9f6b to your computer and use it in GitHub Desktop.
JS terms
  • Call-site: The location in code where a function is called (not where it's declared). Usually the call-site we care about is in the invocation before the currently executing function

  • Call-stack: The stack of functions that have been called to get us to the current moment in execution

  • Closure:

    • When a function still has reference to its outer scope (parameters, variables, functions) even after that outer function has been executed.

    • Closure is when a function is able to remember and access its lexical scope even when that function is executing outside its lexical scope E.g

    function foo() {
        var a = 2;
    
        function bar() {
            console.log(a);
        }
    
        return bar;
    }
    
    var baz = foo();
    
    baz(); // => 2
  • Execution Context (Activation Record):

    • When a function is invoked, an activation record, otherwise known as an execution context, is created.

    • This record contains information about where the function was called from (the call-stack), how the function was invoked, what parameters were passed etc.

    • One of the properties of this record is the this reference which will be used for the duration of that function's execution.

  • Lexical Scope:

    • The value of an object is determined by where it was declared not where it was called from. E.g
    function foo() {
        console.log(a); // => 2
    }
    
    function bar() {
        var a = 3;
        foo();
    }
    
    var a = 2;
    
    bar(); // => 2
  • TDZ (Temporal Dead Zone):

    • Both let and const are hoisted within the lexical scope in which they are defined.

    • They are also declared and initialized to undefined similar to var but the initialization step happens at a later stage.

    • The initialization is not done at the entering of the scope (as with var) but rather happens once the compiler gets to the let statement. Eg let foo

    • If you try to access the let or const variable before this initialization step the browser will throw a ReferenceError

    • This is called a TDZ error; it basically means that you're trying to access a variable before it has been initialized to undefined

    • https://bitsofco.de/variable-and-function-hoisting-in-es2015/#post-2936836202 - Kyle Simpson comment

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