JavaScript - achille005/DevMaterials GitHub Wiki
JavaScript: Understanding the Weird Parts Course:
Notes from- 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 asvar 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
- Ref: Equality_comparisons
- 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 usingthis
to callaMethod()
defined inmyClass
make sure you callbind()
onmyGlobalMethod()
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"
- either on type of a js file ( global) or local to a function.
- when you use "use strict" in your local method to stop this for being a global object (see Section 5, Lecture 13:11)of https://www.udemy.com/javascript-advanced/learn/v4/t/lecture/4673464?start=0
DOM Manupilation:
- document.getElementByClassName("className") VS document.querySelector(".className")
- document.getElementByClassName("idName") VS document.querySelector("#idName")