Giter Site home page Giter Site logo

es6_examples's Introduction

BLOCK SCOPE (LET VAR CONST) EXAMPLES

//Declare a Variable ES5
var user = {
  name: 'Erik',
  password: 'abcd',
  permissions: [ ]
}
console.log(user);
// BLOCK SCOPE WITH LET ///////////////////////////////////////////////////////

if(true){
  let user = {
    name: 'Erik',
    password: 'abcd',
    permissions: [ ]
  };
  console.log(user + ' inside the block');
}
console.log(user + ' outside the block');
// Try to redefine Const
const loggedIn = true;

console.log(loggedIn);

loggedIn = false;

// Now try to change a property in const.
const user = {
  name: 'Erik',
  password: 'abcd'
  permissions: [ ]
};
// change property...
user.password('efgh');
console.log(user);

// now try to update the whole object
user = {
  name: 'Adrian',
  password: 'poop',
  permissions:[]
};
//! ERROR

Default Params Examples

//ES5
let user = {
  name: 'Erik',
  password: 'abcd'
  permissions: [ ]
  updatePassword: function(newPassword){
    this.password = newPassword || 'password';
  }
}

user.updatePassword();
user.updatePassword('banana$$$$');
//Try default params in ES6//////////////////////////////
let user = {
  name: 'Erik',
  password: 'abcd',
  permissions: [ ],
  updatePassword: function(newPassword = 'password'){
    this.password = newPassword;
  }
}
user.updatePassword();
user.updatePassword('banana$$$$');

DESTRUCTURING EXAMPLES

//ex 1
let [newName, newPassword] = ["James", "bunny987"] // show destructuring with array to assign multiple variables
console.log(newName)
console.log(newPassword)

//ex 2
let user = {
  name: 'Erik',
  password: 'abcd',
  permissions: [],
  updatePassword: function(newPassword = 'password'){
    this.password = newPassword;
  }
}
//ES5
console.log('my name is ' + user.name + 'and my password is ' + user.password);

//ES6
function greeting ({name, password}) {
  console.log('my name is ' + name + ' and my password is ' + password);
}
greeting(user);

CONCISE OBJECT PROPERTIES AND METHODS

// EX 1  (shorten method definitions)

//ES5
let user = {
  name: 'Erik',
  password: 'abcd',
  permissions: [],
  updatePassword: function(newPassword = 'password'){
    this.password = newPassword;
  }

  //ES6
  let user = {
    name: 'Erik',
    password: 'abcd',
    permissions: [],
    updatePassword(newPassword = 'password'){
      this.password = newPassword;
      console.log('new pw is: ' + this.password)
    }
  }
user.updatePassword('boop');

// Ex 2 (properties)
let name = 'erik';
let password = 'abcd';
let user = {name, password};
console.log(user);
//TEMPLATE LITERALS////////////////////////////////////////////////////////////////////////////////////////////
let user = {
  name: 'Erik',
  password: 'abcd',
  permissions: [],
  updatePassword(newPassword = 'password'){
    this.password = newPassword;
    console.log('new pw is: ' + this.password)
  }
}

console.log(`Hello. My name is ${user.name} and my password is ${user.password}!`)

//ARROW FUNCTIONS//////////////////////////////////////////////////////////////////////////////////////////////////
let user = {
  name: 'Erik',
  password: 'abcd',
  permissions: [],
  updatePassword(newPassword = 'password'){
    this.password = newPassword;
    console.log('new pw is: ' + this.password)
  },
    autoPost() {
     setInterval( () => {
       console.log(`${this.name}'s Status: Studying Javascript right now and it's tighhhhhht!`)
    }, 1000)
  },
  add(x, y) => x + y;
}
user.autoPost()

es6_examples's People

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.