Saturday, March 23, 2019

ES6 Importent areas with Sample code in Visual Studio code and "Code Runner" Plugin installed to Cisual Studio Code

//import { promises } from "fs";

//import { resolve } from "path";

//import {MOName, MOAge, func} from './moduleTest';
// import p from person;

//########################################
// 1) Install Visual Studio Code
// 2) Install Code Runner from View --> Extension
// 3) Run the Code
//########################################

console.log("test");


//########################################
//Scope
//########################################

var name = "hi";
console.log(name);

console.log("########################################");
console.log("SCOPE Intro");
console.log("########################################");
{
name = "ravika";
age = 10;
}

console.log("Test 1 " + name + " " + age);

{
const name = "satsara";
let age = 40;
console.log("Test 2 " + name + " " + age);
}

console.log("Test 3 " + name + " " + age);


//########################################
//Array
//########################################
console.log("########################################");
console.log("Array");
console.log("########################################");
const num = [1,2,3];
num.push(4);
console.log(num);

//########################################
//Functions
//########################################

//Old method
const multi = function(n){
return n*n;
}

//Inline functions
const type = () => console.log("HI there\nTest in next line");


console.log("Multi " + multi(5));
console.log("Type func " + type()); // This type function is calling, but ths output gives an error.
// due to type() fuction is not return anything. to ignor this,
// you can just take the type() function out from the console.log


const arr1 = (n) => {
return n*n;
}

const arr2 = n => { //If only one parameeters, no need to use the () as (n)
return n*n;
}

const arr3 = (a,b) =>{
return a * b;
}

const arr4 = (a,b) => a * b; // if only one line in the function body

console.log(" Arr1 " + arr1(5));
console.log(" Arr2 " + arr2(5));
console.log(" Arr3 " + arr3(5, 2));
console.log(" Arr4 " + arr4(5, 3));



//########################################
//Function - Default params
//########################################
console.log("########################################");
console.log("Default Params");
console.log("########################################");

const aboutMe = (name, country="Sri Lanka") =>{
console.log("Default para " + name + " " + country);
}

aboutMe("satsara");
aboutMe("Olivier", "Belgium");



//########################################
//Classes
//########################################
console.log("########################################");
console.log("Classes");
console.log("########################################");

//########################################
// Inner Classes
//########################################

class Address {
constructor(city, country, age){
this.city = city;
this.country = country;
this.age = age;
}

static staticPrint()
{
console.log("Static method works!");
}

print(){
console.log("Class Call " + this.city + " " + this.country + "Age:" + this.age);
}
}


class students extends Address { // Inheritance
constructor(city, country, age, grade){
super(city, country, age);// Call super constructor
this.grade = grade;
}

print()
{
console.log("Print Super ");
super.print();
console.log("Print Grade " + this.grade );
}
}

const add = new Address("Homagama", "Sri Lnaka", 40);
add.print();
Address.staticPrint();

const stu = new students("Homagama", "Sri Lnaka", 40, 13);
stu.print();





//########################################
// SET
//########################################
console.log("########################################");
console.log("SET");
console.log("########################################");

const ArraySetNum = new Set([1,2,3,1,2]);
console.log(" Set Value " );
console.log(ArraySetNum);
ArraySetNum.add(10);
console.log(" Set Value 10" );
console.log(ArraySetNum);
ArraySetNum.add(10);
console.log(" Set Value 10 again (No duplicates are added" );
console.log(ArraySetNum);


//########################################
// Object Initializer
//########################################

console.log("########################################");
console.log("Object Initializer");
console.log("########################################");

const newName = "Satsara";
//ES5 OLD version
var person = {
newName: newName,
getAge: function(){
console.log("Ageis 45");
}
};

console.log(" Object is ES5 way " + person.newName);
console.log(" Object is ES5 way function ");
person.getAge();

//ES new Version
const person2 = {
newName, //Removed the newName: newName
getAge(){ // removed the :function() from ES5
console.log("Ageis 45");
}

}

console.log(" Object is ES6 way " + person2.newName);
console.log(" Object is ES6 way function ");
person2.getAge();

//Computerd property name
//########################################
//Dynamic keys for Objects
//########################################
console.log("########################################");
console.log("Dynamic Key Objects");
console.log("########################################");
//############This fails to substitute fruit_var with fruit, not working and wrong
var fruit_var = 'fruit'
var eatables = {fruit_var: 'Apple', vegetable: 'Carrot'}
console.log(eatables) // {fruit_var: 'Apple', vegetable: 'Carrot'}

var eatables = {vegetable: 'Carrot'}
var fruit_var = 'fruit'
eatables[fruit_var] = 'Apple'
console.log(eatables) // {fruit: 'Apple', vegetable: 'Carrot'}


//ES6
var fruit_var = 'fruit'
var vegi_var = 'vegi';
var eatables = {[fruit_var]: 'Apple', vegetable: 'Carrot'}
console.log(eatables) // {fruit: 'Apple', vegetable: 'Carrot'}

//avascript computations using computed property names
var eatables = {[fruit_var]: 'Apple', [fruit_var + ' Cake']: 'yummy',[vegi_var + ' ' + fruit_var]: 'Tomato'}
console.log(eatables) // {fruit: 'Apple', fruit Cake: 'yummy'}

//avascript computations using computed property names
var eatables = {[fruit_var]: 'Apple', [fruit_var + ' Cake']: 'yummy',[vegi_var + ' ' + fruit_var]: 'Tomato', [fruit_var]: 'Banana'}
console.log(eatables) // {fruit: 'Apple', fruit Cake: 'yummy'}

const key = "key" + 1;
const key3 = "keyTest" + 3;

const obj = {
[key]: 'data', // if [], then it must define befor it use
key2: 'data2',// Key2 is
[key3]: 'data3',
};

console.log("Print Key " + obj[key] + " " + obj.key2 + " " + obj.key + " " + obj[key3] );
//*********/obj[key2] not working, obj.key cannot access and it displayied as undifiend


//########################################
// Distructuring
//########################################
console.log("########################################");
console.log("Distructuring");
console.log("########################################");

var person3 = {
name: 'satsara',
age: 45
}

var {name, age} = person3;

console.log('Distructuring ' + name + ' ' + age);

const PersonInfo = ({name, age}) => {
console.log('Function distructuring ' + name.toUpperCase() + ' ' + age);
}

//Calling the distructuring function
PersonInfo(person3);


//########################################
// Distructuring Array
//########################################
console.log("########################################");
console.log("Distructuring Array");
console.log("########################################");

const arr = [6,7,3,4,5];

const [one, two] = arr;

console.log(' Array Distruturing ' + one, two);


//########################################
// Distructuring Array spread operator ...
//########################################

console.log("########################################");
console.log("Spread Operator");
console.log("########################################");


const arrSpread = [1,2,3,4,5];

const arrSpread2 = arrSpread;

console.log('1) Both Arrays are Equals : ' + (arrSpread === arrSpread2));
console.log(arrSpread === arrSpread2);

arrSpread2.push(6);

console.log('2) Both Arrays are Equals : ' + (arrSpread === arrSpread2));

console.log('Added new value 6 to arrSpread2. that auto added to the arrSpread as well');
console.log('arrSpread and arrSpread2, Both accessing the same address (print 6)->' + arrSpread);

console.log('....Now Spred operator...');

const arrSpread3 = [...arrSpread];
console.log(' Array returns fales: because spread operator... ' + (arrSpread3 === arrSpread));


//########################################
// spread operator ... for Objects
//########################################
console.log("########################################");
console.log("Spread Operator for Objects");
console.log("########################################");

const obj2 = {
name:'rice',
price: '123.00'
}

const typ = 'groceries';

const obj3 = {...obj2, typ};

console.log(' Object assign with spread : ');
console.log(obj3);

//########################################
// Modules
//########################################

console.log("########################################");
console.log("Modules, but lines are commented");
console.log("########################################");

//console.log(' From Module ' + MOName);
//func();

//const pe = new p('Ravika',12);
//pe.print();


//########################################
// Promise Object syncronus for Operations
//########################################
console.log("########################################");
console.log("Syncronus / Promise");
console.log("########################################");
//Define the Prmiss Function

const getData = () => {
return new Promise((resolve, reject) => {
setTimeout(()=>{
resolve('Data Found');
},1000);
setTimeout(()=>{
reject('Error found XX');
},100);
});
};

const PromissData = getData();
PromissData.then((result) => {
console.log(result);
}).catch((err) => {
console.log(err);
});

setTimeout(()=>{console.log(' Wait for promis to complete'),1500});

const asyncSum = async (a,b) => {
return a + b;
};


console.log('Async function calling in wrong way' + asyncSum(2,3));
console.log(asyncSum(2,3));

const result = asyncSum(2,3);
result.then(value =>{
console.log(' The correct way to call Async function: ' + value);
});

//########################################
// Promise Object Asyncronus for Operations
//########################################
console.log("########################################");
console.log("Asyncronus / Promise");
console.log("########################################");
const asyncPower = async (a, b) => {
return new Promise((resolve, reject) => {
const r = Math.pow(a,b);
if ((a > 0) && (b > 0))
{
resolve(r);
}
else
{
reject(-1);
}

});
}

asyncPower(2,2).then((val)=>{console.log('Async Result wth asyncPower(2,2) ' + val)}).catch((err ) => {console.log(err)});

asyncPower(0,2).then((val)=>{console.log(val)}).catch((err ) => {console.log('Async Result wth asyncPower(0,2) ' + err)});

const getData2 = async () => {
const data = new Promise((resolve, reject) => {
setTimeout(()=>{
resolve("Data sent OK");
}, 2000);
});

console.log('Test async, timeout 1');
let result = await data;
console.log('Test async, timeout 2');
return result;

}

getData2().then((result)=>{
console.log(result);
}).catch((err)=>{
console.log(err);
});

No comments:

Post a Comment

Postgress - Read a XML file from a postgress table XML column

SELECT xmltable.* FROM xmldata, XMLTABLE('//ROWS/ROW' PASSING data COLUMNS id int PATH ...