Git Product home page Git Product logo

atl-web-082619's Introduction

Helpful ES6 Syntax to Know for React and Beyond ⚛️


The Following are Used heavily in Mod 4 for React (and are important to know if you're writing modern JavaScript):

Destructuring

  // Object Destructuring
  const spaceship = {
    pilot: 'Elon Musk',
    guidance: 'John Cena',
    chef: 'Gordon Ramsay'
  }

  const { pilot, guidance } = spaceship

  console.log(pilot)
  console.log(guidance)

  // Class definition
    class Person {
      constructor(props){
        this.name = props.name
        this.favColor = props.favColor
      }
    }
    // VS
    class Person {
      constructor({name, favColor}){
        this.name = name
        this.favColor = favColor
      }
    }

Key-Value Assignment Shortcuts

  const pizza = 'pepperoni'
  const restaurant = 'dominos'

  const pizzaObj = {
    pizza: pizza,
    restaurant: restaurant
  }

  // VS

  const pizzaObj2 = { pizza, restaurant }

ES6 Spread Operator

  const obj1 = {
    first_name: 'John',
    last_name: 'Doe'
  }

  // Creating an exact copy of the object
  const obj2 = {
    ...obj1,
    favColor: 'Matt Black'
  }

  // OR
  const obj3 = Object.assign({ favColor: 'matt black'},obj1)

  // VS
  const obj4 = obj1
  obj4.favColor = 'Red'
  console.log(obj4)

DIY Filter

  Array.prototype.myFilter = function(callback){
    const filtered = []
    for(let i = 0; i < this.length; i++) {
      const currentElement = this[i]
      if(callback(currentElement)){
        filtered.push(currentElement)
      }
    }
    return filtered
  }

  ['Pizza','Taco','Sandwich','Wrap'].myFilter((snack) => snack === 'Pizza')

All forms of the arrow function

  const implicitReturn = () => 'hi'

  const explicitReturn = () => {
    return 'hi'
  }

Function binding vs arrow function

  const dog = {
    name: 'Scooby',
    favSnack: ['Cheese', 'Peanut Butter', 'Carrots'],
    sayName: function(){
      return this.name
    },
    barkName: () => {
      return this.name + 'BARK!!'
    },
    sayFavFoods: function(){
      this.favSnack.forEach(snack => {
        console.log(`${this.name} likes ${snack}`)
      })
    },
    sayFavFoodsNoArrow: function(){
      this.favSnack.forEach(function(snack){
        console.log(`${this.name} likes ${snack}`)
      })
    }
  }

  dog.sayName() // => ???
  dog.barkName() // => ???
  dog.sayFavFoods() // => ???
  dog.sayFavFoodsNoArrow() // => ???

Class instance properties and class syntax in general

  class Dog{
    constructor(name, age){
      this.name = name
      this.age = age
    }
    woofWoof(){
      return `${this.name} says woof woof!`
    }
  }

Passing function around as callback and Iterators (map, reduce, forEach, filter, find etc..)

  const powerfulPeople = ['Harry','Voldemort','Dumbledore','Grindelwald']

  powerfulPeople.map((name) => name.toLowerCase())

Dynamic Object Keys

  const dynamicSetKeys = (obj, key, val) => {
    obj[key] = val
    return obj
  }

External Resources

atl-web-082619's People

Contributors

evansjwang avatar hsadoqi avatar ipc103 avatar kingcons avatar rocketman5290 avatar whatstheward avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

tostonar raza23

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.