Javascript Flashcards
What does javascript do?
Browser receives html to render. Gets some script code, which executes JS. The script changes what we see on the screen
Ajax requests
Allows a website to communicate with a server asynchronously. I.e. we don’t have to re-render page url to see updates
Node
Allows us to use javascript on the backend.
object in Js
ruby equivalent of a key value hash
Constructor function
defines what our object looks like
.prototype
use it to define methods on a constructor form
new keyword
constant___proto === Obj(Like class on ruby).prototype
node
repl like pry for js
__proto__ vs .prototype
__proto is defined on the instance prototype on the object itself
closure
a function that uses a variable defined in before it
How JS Scope is defined
1 - Arguments
2 - Variables defined in the function
3 - Any variables that were already declared when function was defined (“free variables”)
Closure can also change variables defined before it
What is a callback
A function that is passed into another function as an argument with the intention of being called back at a later time
Common with asynchronous functions. Doesn’t require all functions to finish.
setTimeout
setTimeout
Defined globally
let callback = function() { console.log("It has been 5 seconds!"); }
let timeToWait = 5000
global.setTimeout(callback, timeToWait);
global.setTimeout(function() {
console.log(“It has been 5 seconds!”);
}, 5000) ->ES5
global. setTimeout(() => console.log(“It has been 5 seconds”), 5000);
- > ES6 preferred syntax
callback vs. callback()
() passes in the return value not the invocation of the function
Expressions
line of code that returns a value. Needs a semi-colon at the end