Skip to content

Instantly share code, notes, and snippets.

@asif633
Created September 18, 2017 20:08
Show Gist options
  • Save asif633/cfe40ebfa592a4fbca9d627438885618 to your computer and use it in GitHub Desktop.
Save asif633/cfe40ebfa592a4fbca9d627438885618 to your computer and use it in GitHub Desktop.

Points for let and const

let

  1. No variable hoisting, first declare variable then use. In var you can use variable hoisting.
  2. let is scoping at block level.

const

  1. const is used for constant declarations.
  2. For primitive types the value can't change.
  3. For array and objects as the reference is stored in const, the value can change.

Fat arrow this

  1. The fat arrow function keeps the context where it is defined.
  2. If normal function is called from event listener the this will refer to the html element object.
  3. In case of fat arrow the this will still refer to window object.
  4. No need for bind and call on fat arrow function.

rest and spread

  1. rest is used in arguments where you pass multiple values.
  2. spread is used to spread an array into individual values.

array destructure

let numbers = [1,2,3];
let [a,b] = numbers;
let [x = 'def', ,y] = numbers;
[b,a] = [a,b];
let [c, ...d] = numbers;

object destructure

let obj = {name: 'Asif', age: 21};
let {name, age} = obj;

import and export

other.js

export let as = 'cfv';
export const ds = 12;
export default let ab = 'def';

index.js

import {as,ds} from 'other';
import ab from 'other';

class

class Parent {
  constructor(name){
    this._name = name;
  }
  
  get name(){
    return this._name;
  }
  
  set name(name){
    this._name = name;
  }
  
  greet(){
    console.log('welcome ' + this._name);
  }
}

class Child extends Parent {
  constructor(age){
    super();
    this.age = age;
  }
  
  greet(){
    console.log('welome ' + this._name + 'you are '+ this.age);
  }
  
  static fruit(fruit){
    console.log('my fav fruit is '+ fruit);
  }
}

let parent = new Parent('Asif');
let child = new Child(23);
parent.greet();
child.greet();
Child.fruit('Mango');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment