Skip to content

Javascript concepts

Hernani Fernandes edited this page Jun 11, 2021 · 1 revision

Values & References

  • primitive types: Boolean, null, undefined, String, and Number

  • Reference types: Array, Function, and Object.

  • functions creates a new variables for each parameter

  • equality operators == and ===

Scope vs Context

Scope refers to the visibility of variables

  • let and const keeps the scope

Context refers to the value of this in the same scope (window obj in browser and node obj in app)

  • new logFunc() // creates a local context appoint to the same function
  • if / switch / for /while don’t create contexts so the context will be the same as it’s parent

*JavaScript is a single-threaded language so it can only execute a single task at a time. The rest of the tasks are queued in the Execution Context.

IIFE (Immediately Invokable Function Expressions)

(function(){ alert("Name: " + name); }());

Hoisting

node move the declarations to the top using the browser or node memory space *To avoid hoisting use anonymous function

Closure

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). *Functions that returns functions *closure are promises? Promises are a different concept *Understand that closures allow data hiding, memoization, and dynamic function generation.

This

  • The JavaScript this keyword refers to the object it belongs to:
  • in global object refers to window or node object.
  • in a method the class
  • in a function, this refers to the global object (if it's not strict mode, otherwise is undefined).
  • In an event, this refers to the element that received the event.
  • Methods like call(), and apply() can refer to any object.

New (keyword)

It creates the local this point and adds proto functionalities like a constructor for instance.

Call vs apply vs bind

  • Call you pass as argument the obj that you want as a context from a method - method.call(myNewContext)
  • Apply is the same thing as call but you must send the parameters as a list.
  • Bind is the same as call but returns the method instead of call it.

Pure Functions

Summary of knowledges while I'm studying

Clone this wiki locally