console.log(+"+20"); // 20
==
and ===
==
just compares value===
compares value and type
null == undefined //true
null === undefined //false
- 0
- ""
- false
- null
- undefined
- NaN
- Function declarations
- Var
var
is function scopelet
is block{} scope
Declaration:
function ok(){
console.log("ok");
}
- available in entire scope
- semicolons at the end of function declarations are not necessary; semicolons separate statements however declarations are not statements as they are evaluated before the code is executed (hoisting) Expression
let epic = function ok(){
console.log("ok");
};
epic(); //ok
let cool = function(var){
console.log(var);
}
cool("amazing"); //amazing
//let func = (arg1, arg2, ...argN) => expression
let sum = (a, b) => a + b;
let sum = function(a, b) {
return a + b;
};
const isEven = num = > ( //implicit return
num % 2 == 0
)
const hi = () => alert("hi"); //1 line implicit return
const makeCard = () => ({suit: 'hearts', val: 5});
// implicitly return object literal
// {} implies function body and ({}) is a return value
- A tree (parents, siblings) representing a webpage
- HTML is parsed by a browser to the DOM
- When an event occurs on an element, it runs on itself -> parent -> up to its ancestors
<form onclick="alert('form')">FORM
<div onclick="alert('div')">DIV
<p onclick="alert('p')">P</p>
</div>
</form>
- A click on
<p>
runsonclick
on<p>
-><div>
-><form>
-> ... ->document
- Almost all events bubble
- The element that causes the event is the target element (accessed through
event.target
) - note difference between
event.target
andthis
- Capturing - event goes down to element
- Target - event reached target element
- Bubbling - event bubbles up from element
- Can stop bubbling using
event.stopPropagation()
/event.stopImmediatePropagation()
- No real need to prevent bubbling
- Map, Reduce, Filter, Sort
- Remember to make the function return 1 or -1, that is how sorting works
- "Spreads" array elements; all elements in the array are treated as separate elements being passed through
- Useful for copying arrays/objects as they are reference types
- note: spread only goes 1 level deep, not good for deep copy of nested arrays
nums = [1,2,3,4,5]
Math.min(nums); // NaN
Math.min(...nums) // 1
console.log(..."epic") // e p i c
- String (key), Value pairs
let user = new Object(); // Object Constructor
let user = {}; // Object literal
let user = {
name: "Nick",
age: 19,
"Likes Lisa": true // multiword property
};
alert(user['Likes Lisa']); // remember keys are strings, case-sensitive
delete user;
- A
const
object's properties can change
const user = {
name = 'a';
}
user.name = 'b'
- check if property with key exists
alert('Likes Lisa' in user); // true
for(let i in user){
alert(i); // name, age, Likes Lisa
alert(user[i]); // Nick, 19, true
}
- Keys are listed in creation order, however integers are ordered
for in
is for objects,for of
is for other iterables
- Note
new
keyword
function Book(title, author, read){
this.title = title;
this.author = author;
this.read = read;
}
book1 = new Book('redwall', 'brian jacques', true);
- All objects have a non-enumerable (hidden) prototype property (parent) that is another object or
null
Object.getPrototypeOf/Object.setPrototypeOf
- If we look for a property in a child and it's missing, JS takes it from the prototype (prototype chain)
let animal = {
eats: true
};
let rabbit = {
jumps: true
};
//rabbit.prototype = Object.create(animal.prototype);
Object.setPrototypeOf(rabbit, animal); // obj, prototype
console.log(rabbit.eats); // true
- Iterates through inherited properties
for(let key in rabbit){
console.log(key);
//jumps
//eats
}
this
is not affected by prototypes, is always the object before the
.call()
and.apply
binds the correctthis
value by passing in a scope
- Emulated through Closures (function wrappers; new function = new scope)
- Encapsulation using scope, methods are only available through the
Module
namespace
var Module = (function () {
var myModule = {};
var _privateMethod = function () {
};
myModule.publicMethod = function () {
};
return myModule; // returns the Object with public methods
})();
// usage
Module.publicMethod();
- An inner function has access to the outer function scope
function outer(a){
return function inner(b){
console.log('a is ' + a);
console.log('b is ' +b);
}
}
const newFunction = outer('outside');
newFunction();
//a is outside;
//b is undefined
newFunction('inside');
// a is outside
// b is inside
- Invoke a function expression immediately
- Used for encapsulation
(function () {
var a = 'hey';
console.log(a); // hey
})();
console.log(a); // undefined
constructor()
is automatically called bynew
loadScript('/my/script.js'); // contains function fx()
fx(); // undefined
loadScript
is executed asynchronously, any code after is executed while it is loading- While
loadScript
is loading,fx
runs (undefined!)
loadScript('/my/script.js', () => {
// the callback runs after the script is loaded
fx(); // works
...
});
- Second argument runs when the action is completed
- Nesting looks bad
- Make every action a standalone function (okay fix)
let p = new Promise(function(resolve, reject) {
// do async tasks...
if(/* good */) {
resolve('yay');
} else {
reject('bad');
}
});
p.then(function(result){
// something with result
}).catch(function() {
// uh oh
}).finally(function() {
// executes regardless
})
Another example:
Promise.all([
loadImage(image1),
loadImage(image2),
loadImage(image3)
]).then((images) => {
// do image stuff
}).catch((error) => {
// handle error
})