19 September 2017

ES6 Features


ES6-string-injection

Di ES6 Ada cara mudah memasukan variable ke dalam String. Kalian cukup menggunakan tanda `Put your String here and add your variable like this ${yourVariable}`

var note = notes.addNote(argv.title, argv.body);
if(note){
console.log("successfully saving a new note");
console.log("____");
//ES6 javascript string injection
console.log(`Title: ${note.title}`);
console.log(`Body: ${note.body}`)
}else{
console.log("A note with this title is already exist");
}
view raw gistfile1.js hosted with ❤ by GitHub


Variable 'let'

Perbedaan utama di variable let adalah pada scope-nya. let syntax memungkinkan kita untuk mengimplementasi local variable dengan mudah.

function doSth() {
//tuce is *not* visible out here
for( let i = 0; i < 5; i++ ) {
//i hanya terlihat di scope ini saja
}
//i tidak terlihat disini
}
function byE40() {
//variable i2 terlihat disini
for( var i2 = 0; i2 < 5; i2++ ) {
//var i2 is visible to the whole function
}
//dan juga disini
}
view raw let.js hosted with ❤ by GitHub


Arrow Function

Arrow function sangat memudahkan kita menulis code di javascript. Tapi penggunaan arrow function saya sarankan hanya seandainya kamu sudah tau menulis fungsi tanpa menggunakan arrow function. karena penggunaan arrow function akan membingungkan tanpa memahami proses apa yang terjadi di balik itu.

var square = (x) => {
var result = x*x;
return result;
};
//All can be simplified into one line
//without curly braces means you need the expression to be returned.
var autoReturn = (x) => x * x ;
var withoutReturnValue = (x) => {
console.log(x*x);
};
//With only one argument you can get rid of the prenthesis
var oneArgument = x => x * x;
view raw arrow.js hosted with ❤ by GitHub


Object destructuring

Kalian bisa membuat variable baru dari objects' properties

var user = {
name: 'Peter',
age: 26,
};
// create a new variable name from user object.
var {name} = user;
// with this, you can do such thing like
const {MongoClient, ObjectID} = require('mongodb');
// instead of
const MongoClient = require('mongodb').MongoClient;
const ObjectID = require('mongodb').ObjectID;
view raw objectDest.js hosted with ❤ by GitHub


Promises

Untuk menghandle Asynchronous call, kita bisa menggunakan Promise methode. Cara ini lebih baik karena bisa meminimalisir kesalahan ketika berkerja dengan asynchronous methods.

// initiating a new instance of Promise
// using setTimeout to simulate delay
var asyncAdd = (a, b) => {
return new Promise((resolve, reject) =>{
setTimeout(() => {
if(typeof a === 'number' && typeof b === 'number'){
resolve(a + b);
}else{
reject('Arguments must be numbers');
}
}, 1500)
});
};
//The then method will be called dependent to the result of the somePromise. 1st argument
//will only be called if resolve is called and the 2nd method is called if the reject method had been called.
asyncAdd(5, 7).then((result) => {
console.log('Result: ', result);
}, (errorMessage) => {
console.log('Error: ', errorMessage);
});
// We can also chaining two asynchronous methods like this
// We only provide one error handling method 'catch'
asyncAdd(5, "7").then((result) => {
console.log('Result: ', result);
return asyncAdd(result, 33);
}).then((result) => {
console.log('Should be 45', result)
}).catch((errorMessage) => {
console.log(errorMessage);
});
view raw promise.js hosted with ❤ by GitHub