-
Both are block-scoped, and can only be declared once
-
Function defaults are a thing; can by default set argument to a value
function ok(num=54) { return num } ok() // returns 54 ok('hello') // returns 'hello'
(referred to as reset param in function definition or spread operator in invocation)
-
Use #1: puts the arguments into an actual array
function ok(...stuff){ return Array.isArray(stuff) } // returns true
- note that you can combine multiple params like
function ok(name,...stuff)
and the array will only apply for the 2nd (and later arguments)
- note that you can combine multiple params like
-
Use #2: spreads out array elems inside a function call
ok(a,b,c)
is equivalent took(...[a,b,c])
-
Use #3: spreads out something like array, or string into its constituent parts inside an array:
let name = 'isaac'
[...name,'ibiapina']
[ 'i', 's', 'a', 'a', 'c', 'ibiapina' ] // <---whats returned
let arr = [1,2,3]
[...myarr,4,5]
[ 1, 2, 3, 4, 5 ] // <---whats returned
function build(first, last) {
return {first, last} }
build('isaac','ibiapina')
returns { first: 'isaac', last: 'ibiapina' }
- Pretty much it automatically creates the key name with the same variable name, and evaluates the value of the variable all at once
- Also works by just using variables inside braces, same result achieved by :
first = 'isaac'; last = 'ibiapina'; {first,last}
- Are bound to the scope where they are defined, not called; the
this
is bound to what it was in the definition //nice
- Are shorter/more readable than constructor functions but work the same way
- All the prototype methods are under the same overall function definition
class Mammal {
constructor(name, age, species) {
this.name = name
this.age = age
this.species = species
}
haveBaby(){
//has a baby
}
saysName() {
console.log(this.name)
}
}
extends
keyword is used to create a child class and inherit methods & properties from the parentsuper
keyword is included on the FIRST line of the child class's constructor function so the properties from the parent are inherited- also it can be used for something else when a child class wants to use a parent's method (details TBD)
class Dog extends Mammal {
constructor(favFood, breed){
super()
}
}