scrimba
Javascript Hacks with ES6-ES7
JavaScript Hack with ES6 -ES7 #20
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

JavaScript Hack with ES6 -ES7 #20
AboutCommentsNotes
JavaScript Hack with ES6 -ES7 #20
Expand for more info
index.js
run
preview
console


var object = {
a : 50,
b: 60
}
var x = JSON.parse(JSON.stringify(object));
console.log(x);

// timer methods from web API

setTimeout(function(){
console.log('after 5 sec');
}, 5000);

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(this.readyState == 4){
clearTimeout(timeout);
}
}
var timeout = setTimeout (function(){
xhr.abort();
}, 2000);

xhr.open('GET', url, true);
xhr.send();

//----------------------------------------------//

function hello(a,b) {
return a+b;
}

function add(a){
return function(b){
return function(c){
return a+b+c;
}
}
}
add(4)(5)(8);

var x = add(4);
var y = x(5);
var z = y(8);

/************************************************* */

var keywords = ["do","if","else","for" ,"switch"];

keywords
.filter( i => i.length > 3 )
.map( i => i.concat('Hi') )
.sort( (a,b) => a < b ? -1 : 1);

/************************************************ */

class ChainAble {
firstMethod() {
console.log('This is the First Method');
return this;
}

secondMethod() {
console.log('This is the Second Method');
return this;
}

thirdMethod() {
console.log('This is the Third Method');
return this;
}
}

const chainableInstance = new Chainable()
chainableInstance
.firstMethod()
.secondMethod()
.thirdMethod();

/**************************************************** */

var sayHello = function ({name, surname}){
console.log(`hello ${name} ${surname}`);
}
var sayHello2 = function ({name = "tarun", surname = "sharma"}, times){
console.log(`hello ${name} ${surname} ${times}`);
}

sayHello({name : 'tks'});
sayHello();
sayHello(566);


var x = {};

Object.defineProperty(x,'readOnly' , {
value : 56,
writable : false,
enumerable : true,
configurable : false
})
Object.getOwnPropertyDescriptor(x, 'readOnly');
console.log(x);

//-------------------------------------------------------------------//
// remove duplicates from Array

var array = [4,5,6,7,8,8,6,7,8];

array.filter(function(item, index, array){
return array.indexOf(item) === index
});

var x = [... new Set(array)];
Array.from(new Set(array));


var object = {
a : 70,
b : 70,
c : {
a :80
}
};


function duplicate(err){
var hashTable = {};
return arra.filter(function(el){
var key = JSON.stringify(el);
var match = Boolean(hashTable[key]);

return (match ? false : hashTable[key] = true);
})
}
var array = [
{ a :1},
{a :1 },
[1,2],
[1,2]
]
duplicate(array);


/************************************************************ */
//Flatten an Array
var myarray = [ [1,2], [4,5,6,7],[6,7,8,9]];


Console
{a:
50
, b:
60
}
,
"after 5 sec"
,
index.html
-3:28