Tuesday, August 21, 2018

Next Generation JavaScript (Part-1)


  • Variable declaration
var oldVariable='shadhin';
let NewVariable="Monir";
console.log(oldVariable);
console.log(NewVariable);
const variableCanotChange=10;
console.log(variableCanotChange);
//variableCanotChange=20;
console.log(variableCanotChange);


  • Function
function oldfunction(para) {
    console.log(para);
}
let newFunction=(para)=>{
    console.log(para);
}
//if only one parameter
 newFunction=para=>{
    console.log(para);
}
//if no parameter
 newFunctionWithoutPara=()=>{
    console.log("without para");
}
//multiple parameter
functionMultiplePara=(name,age)=>{
    console.log(name,age);
    console.log(age*2);
}
//function with short form
const shortFun=para=>para*2;

oldfunction("shadhin");
newFunction("monir1");
newFunction("monir2");
newFunctionWithoutPara();
functionMultiplePara('shadhin',10);
console.log(shortFun(20));


  • Export Import

  • class
class a {
    constructor(){
        this.age=20;
    }
    print_age(){
        console.log(this.age)
    }
}
class b extends a{
    constructor(){
        super();
        this.age=27;
        this.name='shadhin';
    }
    print_name(){
        console.log(this.name);
    }
}
const obj=new b();
obj.print_name();
obj.print_age();


  • ES6 class

class a {
    age=20;
    print_age=()=>{
        console.log(this.age)
    }
}
class b extends a{
    age=27;
    name='shadhin';
    print_name=()=>{
        console.log(this.name);
    }
}
const obj=new b();
obj.print_name();
obj.print_age();


  • Spread & Rest

//Spread operator
const numbers =[1,2,3];
const newNumbers=[...numbers,4];
console.log(newNumbers);
const obj={
    name:'shadhin'
}
const newobj={
    ...obj,
    age:28
}
console.log(newobj);

//Rest
const sortFun=(...arg)=>{
    return arg.sort();
}
console.log(sortFun(2,4,1,6,8));


  • Destructuring

const numbers=[1,2,3];
[num1,,num3]=numbers;
console.log(num1,num3);


  • variable copy (Array and Object Reference type)





  • Built in function return 



No comments:

Post a Comment