Keyword “this”

JavaScript engine every time an execution context is created, that is every time a function is RUN, it gives a variable called “this” pointed to a different object depends on how the function is invoked(where the function is and how it been called)

Weird part

 

發表留言

By Value v.s. By Reference

by value (primitive type)

copying the value into two separate spots in memory

by reference (all objects)

no copy of object is created, instead, two names or more point to same address

發表留言

Function in JavaScript

JavaScript functions are objects that has other special properties:

  • Name: optional, can be anonymous
  • Code: invokable, you can RUN these code by ()
Function statement

Create a object with NAME attribute and point to memory in the creation of execution context

Function expression (anonymous)

Create a object without NAME(already have a variable knows where this object lives) attribute and point to memory in the creation of execution context, and assign this object to function when this line of code is execute

Immediately Invoked Function Expressions (IIFE)

Create the function object on the fly and invoke it immediately, and assign what it return to the variable.

Why use iife and why it is safe

Code wrapped in IIFE does not interfere with, crash into, or be interfered by any other code included in the application.

Methods

In one function’s execution context, we have 1. Variable Environment 2. Outer Environment 3. “this”.

Q: How to control “this” variable?

JavaScript functions also has these methods(can control “this”):

  • call()
  • apply()
  • bind()

Functional Programming

We should think and code in terms of functions

發表留言

Type in JavaScript

primitive value
  • undefined
  • null
  • boolean
  • number (only one number type…)
  • string
  • symbol (ES6)
coercion

expected

weird parts

loose equal vs strict equal

use === to prevent coercion and do not use == in 99% cases except you mean to coerce

ref:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness

發表留言

Event Queue

When the browser, somewhere outside the JavaScript engine, has the event inside the JavaScript engine that want to be notified of, its get placed in the event queue, and WHETHER OR NOT the event have a function response to it.

The event queue gets looked at by JavaScript when the execution stack is EMPTY.

JavaScript engine is always running synchronously, what happening is that the browser is asynchronously putting things into event queue.

 

發表留言

let in ES6

let allows the JavaScript engine to use BLOCK SCOPING, means variables are only allowed inside specific block, such as if/else statement which defined by {}

發表留言