Friday, 21 December 2018

1.     Default parameters:
Before ES6 we had to do these statements to define default parameters:
Text Box: var link = function (height, color, url) {
    var height = height || 50
    var color = color || 'red'
    var url = url || 'http://azat.co'
    ...
}





Text Box: var link = function(height = 50, color = 'red', url = 'http://azat.co') {
  ...
}
They were okay until the value was 0 and because 0 is false in JavaScript it would default to the hard-coded value instead of becoming the value itself. Of course, who needs 0 as a value .so we just ignored this flaw and used the logical OR. we can put the default values right in the signature of the functions:





2.     Template Literals
Template Literals or interpolation is a way to output variables in the string.
Text Box: var name = 'Your name is ' + first + ' ' + last + '.'
var url = 'http://localhost:3000/api/messages/' + id


In ES6, we can use new syntax ${Name} in the back ticked string.
E.g.:
var name = `Your name is ${first} ${last}.`
var url = `http://localhost:3000/api/messages/${id}`

3.     Text Box: var roadPoem = 'Then took the other, as just as fair,\n\t'
    + 'And having perhaps the better claim\n\t'
    + 'Because it was grassy and wanted wear,\n\t'
    + 'Though as for that the passing there\n\t'
    + 'Had worn them really about the same,\n\t'

var fourAgreements = 'You have the right to be you.\n\
    You can only be you when you do your best.'
Multiline Strings
In ES5, to declare a string in multiple lines we had to use one of the below two approaches:
While in ES6, simply utilize the backticks:
var roadPoem = `Then took the other, as just as fair,
    And having perhaps the better claim
    Because it was grassy and wanted wear,
    Though as for that the passing there
    Had worn them really about the same,`

var fourAgreements = `You have the right to be you.
    You can only be you when you do your best.`
4.     De-structuring assignment
var data = $('body').data(), // data has properties house and mouse
  house = data.house,
  mouse = data.mouse
Other examples of destructuring assignments (from Node.js):
var jsonMiddleware = require('body-parser').json
var body = req.body, // body has username and password
  username = body.username,
  password = body.password 
In ES6, we can replace the ES5 code above with these statements:

var {house, mouse} = $('body').data() // we'll get house and mouse variables
var {json: jsonMiddleware} = require('body-parser')
var {username, password} = req.body
This also works with arrays. Crazy!
var [col1, col2]  = $('.column'),
  [line1, line2, line3, , line5] = file.split('\n')
5.     Enhanced Object Literal
In ES5
In ES6
6.     Arrow functions
Using arrows functions in ES6 allows us to stop using that = this or self = this or _this = this or .bind(this). For example, this code in ES5 is ugly:
Text Box: var _this = this
$('.btn').click(function(event){
  _this.sendData()
})





This is the ES6 code without _this = this:
Text Box: $('.btn').click((event) =>{
  this.sendData()
})





7.     Promises
There were a lot of promise implementations with slightly different syntax. q, bluebird, deferred.js, vow, avow, jquery deferred to name just a few. Others said we don’t need promises and can just use async, generators, callbacks, etc. Gladly, there’s a standard Promise implementation in ES6 now!

Let’s consider a rather trivial example of a delayed asynchronous execution with setTimeout():
Text Box: setTimeout(function(){
  console.log('Yay!')
}, 1000)




We can re-write the code in ES6 with Promise:
Text Box: var wait1000 =  new Promise(function(resolve, reject) {
  setTimeout(resolve, 1000)
}).then(function() {
  console.log('Yay!')
})





8.     Block scoped construct let and const
9.     Text Box: class baseModel {
  constructor(options = {}, data = []) { // class constructor
    this.name = 'Base'
    this.url = 'http://azat.co/api'
    this.data = data
    this.options = options
  }
    getName() { // class method
     console.log(`Class name: ${this.name}`)
    }
}
Classes

10.Modules: Native Module support
As you might now, there were no native modules support in JavaScript before ES6. People came up with AMD, RequireJS, CommonJS and other workarounds. Now there are modules with import and export operands.


Wednesday, 12 December 2018

You are responsible for your life/love. You can't keep blaming somebody else for your dysfunction. Life is really about moving on ..

So take responsibility and be happy