JavaScript - achille005/DevMaterials GitHub Wiki

Notes from JavaScript: Understanding the Weird Parts Course:

  • There are 6 primitive types in javaScript: Undefined, NULL, BOOLEAN, NUMBER, String and Symbol(used in ES6)
    • javaScript follow dymanic types not a static like other language
  • var person = new Object(); is the same as var person = {};
  • global execution context in javaScript are like main in java. Local variables overwrite global ones
  • JavaScript execute action or function in the order they happen.
  • In general use === or !== (instead of == and != ) to compare primitives
  • In JS functions are objects
  • In JS primitive are assigned by value ( each variable hold its own copy) - Objects by reference ( each var points to the same object.
  • this key word in JS always represents the outer object (not the necessarily the inner in which it resides) use "var self = this" inside of local functions/objects and use self in the rest of the code to make sure you are still pointing to the global "this"
  • JS array can hold anything, functions, numbers, strings,... because of its dynamics typing capability.
  • There is no function overloading in JS like you to in Java (you can't use the same function name with different number of arguments)
  • IIFE (function(){consol.log("Hi") }()); -How to affect a global object from an IIFE (see Bookmark)
  • Closure function sayHiLater() { var greeting = 'Hi!'; setTimeout(function() { console.log(greeting); }, 3000); }

setTimeout() is a closure to 'var greeting'

  • Callback

function tellMeWhenDone(callback) { var a = 1000; // some work var b = 2000; // some work callback(); // the 'callback', it runs the function I give it! }

tellMeWhenDone(function() { console.log('I am done!'); });

tellMeWhenDone(function() { console.log('All done...'); });

  • Given myGlobalMethod() is in global environment, when using this to call aMethod() defined in myClass make sure you call bind() on myGlobalMethod() e.g. myGlobalMethod(){ this.aMethod()}.bind(myClass) See bookmark on the video

  • bind() Call() and apply()

bind() returns a function - call() takes as first argument the object to which it being bound to then any number of arguments (e.g myGlobalMethod(){ this.aMethod()}.bind(myClass, arg1, arg2, argn) ) but apply() takes an array of arguments as the second argument (e.g myGlobalMethod(){ this.aMethod()}.bind(myClass, [] )

Function borrowing

Use apply() to use a function of another class on similar object - see bookmark

Function Currying

using bind() to make a copy of a function with a pre-set parameter - see multiplyByTwo example in the lecture

Function Constructors and Prototypes

Methods added to the prototypes of a parent function prototypes before or after the function is created will be made available to the children function objects

extend keyword

from underscore.js library to add other object members to another one.

Moment.js.com

Recommended over JS built-in Date to manipulate Date.

Oject.prototype.toString.call(myObject) ===> "[some array object]"

typeof vs instanceof

  • instanceof check to see if that object is in the prototype chaine

Two uses of "use strict"

DOM Manupilation:

  • document.getElementByClassName("className") VS document.querySelector(".className")
  • document.getElementByClassName("idName") VS document.querySelector("#idName")