Skip to content

Instantly share code, notes, and snippets.

@eyezick
Last active March 23, 2017 00:47
Show Gist options
  • Save eyezick/664ea93875456cf117fdc9307b702056 to your computer and use it in GitHub Desktop.
Save eyezick/664ea93875456cf117fdc9307b702056 to your computer and use it in GitHub Desktop.
ES6 Notes

const & let:

  • 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'
    

Named parameters- don't see the use for it

the three dots

(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)
  • Use #2: spreads out array elems inside a function call

    • ok(a,b,c) is equivalent to ok(...[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

Quick object initializer (im likin it):

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}

Arrow functions

  • Are bound to the scope where they are defined, not called; the this is bound to what it was in the definition //nice

Classes

  • 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)
   }
 
 }

Class Inheritance

  • extends keyword is used to create a child class and inherit methods & properties from the parent
  • super 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()
  
   }
 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment