├── api └── test.js ├── README.md ├── promises └── test.js ├── proxies └── test.js ├── reflectApi └── test.js ├── symbols └── test.js ├── tailCalls └── test.js ├── .gitignore ├── moduleLoaders └── test.js ├── subclassableBuiltIns └── test.js ├── enhancedObjectLiterals └── test.js ├── generators ├── runner-compiled.js ├── p1.js ├── generators.js └── p2.js ├── arrow ├── p3.js ├── p1.js ├── p2.js └── p4.js ├── modules ├── demo_export.js └── demo_import.js ├── dataTypes ├── const.js ├── loop.js ├── constUse.js ├── symbol_datatype.js ├── constants.js ├── scope.js └── constUse2.js ├── map&set ├── weekmap.js ├── set.js ├── weekset.js └── map.js ├── destructing ├── default_parameters.js ├── p2.js ├── p1.js ├── p4.js ├── parameter_rest.js ├── P3.js ├── p5.js └── destructuring.js ├── examples ├── demo_export.js └── demo_import.js ├── class ├── static_in_classes.js ├── p5.js ├── p2.js ├── p3.js ├── p4.js ├── inheritence_demo.js ├── p6.js ├── p1.js └── multi_level_inheritance.js ├── templating ├── func.js └── template.js ├── iterator ├── main.js ├── iterators.js └── iterator.js ├── misc ├── template_literals.js ├── p1.js └── p2.js ├── getter_setter └── getter_setter.js └── scope ├── scope_function.js └── scope_variable.js /api/test.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ES6 -------------------------------------------------------------------------------- /promises/test.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /proxies/test.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /reflectApi/test.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /symbols/test.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tailCalls/test.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | -------------------------------------------------------------------------------- /moduleLoaders/test.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /subclassableBuiltIns/test.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /enhancedObjectLiterals/test.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /generators/runner-compiled.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /arrow/p3.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/29/16. 3 | */ 4 | const emptyObject = () => ({}); 5 | emptyObject(); // {} 6 | /** 7 | * Created by princesoni on 1/30/16. 8 | */ 9 | -------------------------------------------------------------------------------- /arrow/p1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/30/16. 3 | */ 4 | var x = function (a) { 5 | return a; 6 | } 7 | 8 | var x = a => { 9 | console.log("other thing"); 10 | return a; 11 | } 12 | x() 13 | -------------------------------------------------------------------------------- /modules/demo_export.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by priya on 27/4/16. 3 | */ 4 | export function sum (x, y) { return x + y }; 5 | export var pi = 3.141593; 6 | export default function(){ 7 | console.log("this is a default module"); 8 | } 9 | -------------------------------------------------------------------------------- /dataTypes/const.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/29/16. 3 | */ 4 | 'use strict' 5 | const x = 0; 6 | 7 | console.log(x) 8 | 9 | if(true){ 10 | const y =100 11 | } 12 | //Eror y is not defined 13 | console.log(y) 14 | -------------------------------------------------------------------------------- /map&set/weekmap.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/27/16. 3 | */ 4 | //https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/WeakMap 5 | var person = new WeakMap(); 6 | var obj = {name: 'prince'}; 7 | person.set(obj, 'test') 8 | console.log(person.get(obj)) -------------------------------------------------------------------------------- /destructing/default_parameters.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by priya on 30/4/16. 3 | */ 4 | //default values in functions 5 | function f (x, y=2, z = 3) { 6 | console.log(x,y,z); //1 2 3 7 | return x + y + z; 8 | } 9 | console.log(f(1) === 6) //true 10 | -------------------------------------------------------------------------------- /examples/demo_export.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by priya on 27/4/16. 3 | */ 4 | //export * from "demo" not working 5 | 6 | export function sum (x, y) { return x + y }; 7 | export var pi = 3.141593; 8 | export default function(){ 9 | console.log("this is a default module"); 10 | } 11 | -------------------------------------------------------------------------------- /class/static_in_classes.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by priya on 28/4/16. 3 | */ 4 | class Shape { 5 | static a(){ 6 | console.log("Static method"); 7 | } 8 | } 9 | Shape.a(); //Static method 10 | 11 | var obj=new Shape(); 12 | // obj.a(); //error: obj.a a is not defined 13 | Shape.a(); //hello 14 | -------------------------------------------------------------------------------- /templating/func.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/30/16. 3 | */ 4 | 'use strict' 5 | function getData(string, args) { 6 | console.log(string[0]); 7 | console.log(string[1]); 8 | console.log(args[0]); 9 | console.log(args[1]); 10 | } 11 | 12 | getData` user is ${1} not present ${2}`; 13 | -------------------------------------------------------------------------------- /class/p5.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/29/16. 3 | */ 4 | var Vehicle = class { 5 | } 6 | 7 | var Vehicle = class VehicleClass { 8 | constructor() { 9 | // VehicleClass is only available inside the class itself 10 | } 11 | } 12 | 13 | console.log(VehicleClass); // throws an exception 14 | -------------------------------------------------------------------------------- /iterator/main.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/27/16. 3 | */ 4 | 'use strict' 5 | let arr = [3, 5, 7]; 6 | arr.foo = "hello"; 7 | 8 | for (let i in arr) { 9 | console.log(i); // logs "0", "1", "2", "foo" 10 | } 11 | 12 | for (let i of arr) { 13 | console.log(i); // logs "3", "5", "7", "hello" 14 | } 15 | -------------------------------------------------------------------------------- /misc/template_literals.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by priya on 28/4/16. 3 | */ 4 | var card = { 5 | amount: 7, 6 | product: "Bar", 7 | unitprice: 42 8 | } 9 | 10 | var message = `Hello ${card.amount+2} and price ${card.unitprice+1}`; 11 | 12 | console.log(message); //hello 9 and price 43. 13 | 14 | 15 | -------------------------------------------------------------------------------- /map&set/set.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/27/16. 3 | */ 4 | 5 | //https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Set 6 | var person = new Set(); 7 | 8 | person.add('prince') 9 | person.add('soni') 10 | person.add({name: 123}); 11 | console.log(person) 12 | console.log(person.has('prince')) -------------------------------------------------------------------------------- /class/p2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/30/16. 3 | */ 4 | 5 | var Person = class { 6 | constructor(name) { 7 | this.name = name; 8 | } 9 | 10 | getName() { 11 | console.log("name of the person " , this.name); 12 | } 13 | } 14 | var person = new Person('prince soni'); 15 | person.getName(); 16 | 17 | -------------------------------------------------------------------------------- /dataTypes/loop.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/29/16. 3 | */ 4 | 'use strict' 5 | //using for 6 | for (var i = 0; i <= 3; i++) { 7 | console.log(i) 8 | } 9 | console.log('outside value of i', i); 10 | 11 | 12 | //using let 13 | for (let j = 0; j <= 3; j++) { 14 | console.log(j) 15 | } 16 | console.log('outside value of j', j); 17 | -------------------------------------------------------------------------------- /destructing/p2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/30/16. 3 | */ 4 | 'use strict' 5 | 6 | var arr = [1,2,3, [5,6], 5, 4]; 7 | var six = arr[3] 8 | console.log(six); 9 | 10 | var [one, two, three, six] = arr; 11 | console.log(six) 12 | 13 | var [, , , six] = arr; 14 | console.log(six) 15 | 16 | var [one, ...args] = arr; 17 | console.log(args) 18 | -------------------------------------------------------------------------------- /iterator/iterators.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by priya on 29/4/16. 3 | */ 4 | let iterable = [3, 5, 7]; 5 | iterable.foo = "hello"; 6 | 7 | for (let i in iterable) { //index of array 8 | console.log(i); // 0, 1, 2, "foo" 9 | } 10 | 11 | for (let i of iterable) { 12 | console.log(i); // 3, 5, 7 //elements of array 13 | } 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /destructing/p1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/30/16. 3 | */ 4 | 'use strict' 5 | var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9] 6 | var five = arr[4]; 7 | var eight = arr[6]; 8 | 9 | console.log(five); 10 | console.log(eight); 11 | 12 | 13 | var [one, two, three] = arr; 14 | console.log(one); 15 | console.log(two); 16 | console.log(three); 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /generators/p1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/30/16. 3 | */ 4 | 5 | function* getData() { 6 | var a = yield a; 7 | console.log(a) 8 | var b = yield b; 9 | console.log(b) 10 | var c = yield c; 11 | console.log(c) 12 | var d = yield d; 13 | console.log(d) 14 | } 15 | 16 | var it = getData(); 17 | console.log(it.next()); 18 | -------------------------------------------------------------------------------- /class/p3.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/30/16. 3 | */ 4 | 5 | var person = new Person('prince soni');//Error won't work 6 | person.getName(); 7 | 8 | var Person = class { 9 | constructor(name) { 10 | this.name = name; 11 | } 12 | 13 | getName() { 14 | console.log("name of the person " , this.name); 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /examples/demo_import.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by priya on 27/4/16. 3 | */ 4 | 'use strict' 5 | 6 | import example_default,{sum,pi} from "./demo_export.js"; 7 | /* 8 | Also used as 9 | 1 ) import {default as example_default,sum,pi} from "./demo_export.js"; 10 | */ 11 | console.log(sum(pi,pi)); //6.283186 12 | example_default(); //this is default module 13 | 14 | -------------------------------------------------------------------------------- /modules/demo_import.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by priya on 27/4/16. 3 | */ 4 | 'use strict' 5 | 6 | import example_default,{sum,pi} from "./demo_export.js"; 7 | /* 8 | Also used as 9 | 1 ) import {default as example_default,sum,pi} from "./demo_export.js"; 10 | */ 11 | console.log(sum(pi,pi)); //6.283186 12 | example_default(); //this is default module 13 | 14 | -------------------------------------------------------------------------------- /dataTypes/constUse.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/29/16. 3 | */ 4 | 'use strict' 5 | const x = 100; 6 | //x = 200;// error constant cannot be re-assigned 7 | 8 | const obj = {} 9 | obj.name = 'prince soni' 10 | //obj = {} // error constant cannot be re-assigned 11 | 12 | const arr = []; 13 | arr.push(100); 14 | //arr = []; // error constant cannot be re-assigned 15 | -------------------------------------------------------------------------------- /destructing/p4.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/30/16. 3 | */ 4 | var arr = [1,2,3] 5 | var one = arr[0] 6 | var two = arr[1] 7 | console.log(one, two) 8 | 9 | //swapping 10 | var temp ; 11 | temp = one; 12 | one = two; 13 | two = temp; 14 | console.log(one, two) 15 | 16 | //es6 17 | var [one1, two1] = arr; 18 | console.log(one1, two1); 19 | [one1, two1] = [two1, one1]; 20 | console.log(one1, two1); 21 | -------------------------------------------------------------------------------- /class/p4.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/30/16. 3 | */ 4 | 5 | class Person{ 6 | constructor(name) { 7 | this.name = name; 8 | } 9 | 10 | getName() { 11 | console.log("name of the person " , this.name); 12 | } 13 | } 14 | 15 | class child extends Person{ 16 | constructor(name){ 17 | super(name) 18 | } 19 | } 20 | 21 | 22 | var person = new child('prince soni'); 23 | person.getName(); 24 | 25 | -------------------------------------------------------------------------------- /arrow/p2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/27/16. 3 | */ 4 | function () { return 1; } 5 | () => { return 1; } 6 | () => 1 7 | 8 | function (a) { return a * 2; } 9 | (a) => { return a * 2; } 10 | (a) => a * 2 11 | a => a * 2 12 | 13 | function (a, b) { return a * b; } 14 | (a, b) => { return a * b; } 15 | (a, b) => a * b 16 | 17 | function () { return arguments[0]; } 18 | (...args) => args[0] 19 | 20 | () => {} // undefined 21 | () => ({}) // {} 22 | -------------------------------------------------------------------------------- /destructing/parameter_rest.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by priya on 28/4/16. 3 | */ 4 | function f (x, y, ...a) { 5 | 6 | console.log("helo") 7 | console.log("x:",x,"y:",y,"...a",a) // x: 1 y: 2 ...a [ 'hello', true, 7 ] 8 | return (x + y) * a.length; 9 | } 10 | console.log(f(1, 2, "hello", true, 7)==9); //true 11 | 12 | 13 | var str = "foo"; 14 | var chars = [ ...str ] // [ "f", "o", "o" ] 15 | console.log(chars); 16 | var char=[str]; 17 | console.log(char); //["foo"] 18 | -------------------------------------------------------------------------------- /map&set/weekset.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/27/16. 3 | */ 4 | //https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/WeakSet 5 | var ws = new WeakSet(); 6 | var obj = {}; 7 | var foo = {}; 8 | 9 | //ws.add(window); 10 | ws.add(obj); 11 | 12 | //ws.has(window); // true 13 | console.log(ws.has(obj)); // false, foo has not been added to the set 14 | 15 | //ws.delete(window); // removes window from the set 16 | //ws.has(window); // false, window has been removed 17 | -------------------------------------------------------------------------------- /destructing/P3.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/30/16. 3 | */ 4 | var obj = { 5 | name: 'prince soni', 6 | city: 'noida' 7 | } 8 | 9 | var name = obj.name; 10 | console.log(name); 11 | 12 | var {name} = obj; 13 | console.log(name); 14 | 15 | var obj = { 16 | name: 'prince soni', 17 | city: 'noida', 18 | address : [1,2,3], 19 | location: { 20 | 21 | } 22 | } 23 | 24 | //check for undefined value 25 | var {location: {country}} =obj; 26 | console.log(country); 27 | 28 | 29 | -------------------------------------------------------------------------------- /misc/p1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/27/16. 3 | */ 4 | 5 | //Default arguments in es6 6 | function f(x, y=12) { 7 | // y is 12 if not passed (or passed as undefined) 8 | return x + y; 9 | } 10 | f(3) == 15 11 | 12 | 13 | //variable length for array - rest 14 | function f(x, ...y) { 15 | // y is an Array 16 | return x * y.length; 17 | } 18 | f(3, "hello", true) == 6 19 | 20 | //Spread 21 | function f(x, y, z) { 22 | return x + y + z; 23 | } 24 | // Pass each elem of array as argument 25 | f(...[1,2,3]) == 6 26 | -------------------------------------------------------------------------------- /map&set/map.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/27/16. 3 | */ 4 | //https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map 5 | 6 | var person = new Map(); 7 | //set to map 8 | person.set('name', 'prince'); 9 | 10 | //get from map 11 | console.log(person.get('name')) 12 | 13 | //size of map 14 | console.log(person.size); 15 | 16 | //entries 17 | console.log(person.entries()) 18 | 19 | //delete all 20 | //person.clear(); 21 | console.log(person.size); 22 | 23 | //delete specific 24 | person.delete('name') 25 | console.log(person.entries()) -------------------------------------------------------------------------------- /arrow/p4.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/30/16. 3 | */ 4 | 5 | 'use strict'; 6 | 7 | 8 | 9 | function user() { 10 | this.name = 'prince'; 11 | setTimeout(function () { 12 | console.log("### executed ###", this.name); 13 | }, 3000) 14 | } 15 | var person = new user(); 16 | 17 | console.log('output ... ',person); 18 | 19 | 20 | /*function user() { 21 | this.name = 'prince'; 22 | setTimeout(() => { 23 | console.log("### executed ###", this.name); 24 | }, 3000) 25 | } 26 | var person = new user(); 27 | 28 | console.log('output ... ',person);*/ 29 | -------------------------------------------------------------------------------- /class/inheritence_demo.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by priya on 27/4/16. 3 | */ 4 | class Shape{ 5 | constructor(id,x,y){ 6 | this.id=id; 7 | this.x=x; 8 | this.y=y; 9 | } 10 | func(id,x,y){ 11 | console.log(this.id,this.x,this.y); 12 | } 13 | } 14 | 15 | 16 | 17 | class Rectangle extends Shape { 18 | constructor (id, x, y, width, height) { 19 | super(id, x, y); 20 | this.width = width; 21 | this.height = height; 22 | super.func(); //1 2 3 23 | } 24 | } 25 | 26 | var obj=new Rectangle(1,2,3,4,5); 27 | obj.func(4,5,6); //1 2 3 28 | -------------------------------------------------------------------------------- /class/p6.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/30/16. 3 | */ 4 | 5 | var Person = class { 6 | constructor(name) { 7 | this.name = name; 8 | } 9 | 10 | static getName() { 11 | console.log("name of the person " , this.name); 12 | } 13 | } 14 | Person.getName(); 15 | 16 | /* 17 | var Person = class { 18 | 19 | constructor(name) { 20 | this.name = name; 21 | } 22 | 23 | */ 24 | /*static*//* 25 | getName() { 26 | console.log("name of the person " , this.name); 27 | } 28 | } 29 | //Person.getName(); 30 | 31 | var obj = new Person('akash'); 32 | 33 | obj.getName();*/ 34 | -------------------------------------------------------------------------------- /getter_setter/getter_setter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by priya on 27/4/16. 3 | */ 4 | class Rectangle { 5 | constructor (width, height) { 6 | this._width = width; 7 | this._height = height; 8 | } 9 | set width (width) { 10 | this._width = width; 11 | } 12 | get width () { 13 | return this._width; 14 | } 15 | set height (height) { 16 | this._height = height; 17 | } 18 | get height () { 19 | return this._height; 20 | } 21 | get area () { 22 | return this._width * this._height; 23 | } 24 | } 25 | var r = new Rectangle(50, 20); 26 | console.log("Area of rectangle",r.area); 27 | -------------------------------------------------------------------------------- /destructing/p5.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/30/16. 3 | */ 4 | 'use strict' 5 | 6 | function calAverage({hindi: h, english:e, science:s, minmark = 9, callback:cb}) { 7 | if (minmark < 1) { 8 | console.log("your are disqualified in exam..") 9 | } 10 | var avg = (h + e + s) / 3; 11 | if (avg > 1) { 12 | console.log("Student is passed in the examination"); 13 | } 14 | if (cb) 15 | cb(avg) 16 | } 17 | 18 | calAverage(1, 2, 3, 1, function (res) { 19 | console.log("Result occured") 20 | }); 21 | calAverage({ 22 | hindi: 1, english: 2, science: 3, callback: function () { 23 | console.log("pp") 24 | } 25 | }); 26 | -------------------------------------------------------------------------------- /dataTypes/symbol_datatype.js: -------------------------------------------------------------------------------- 1 | 2 | var a=function(){ 3 | return 1; 4 | }; 5 | var obj={ 6 | [a()+1]:20 7 | } 8 | console.log(obj[2]); //20 9 | //console.log(obj.2); error unexpected token 10 | 11 | //Using Symbol data type 12 | var firstName = Symbol("first name"); 13 | var person = { 14 | age:1 15 | }; 16 | person[firstName] = "Nicholas"; 17 | console.log("firstName" in person); // false 18 | console.log("first name" in person); //false 19 | console.log(person[firstName]); // "Nicholas" 20 | console.log(firstName); //Symbol(first name) 21 | console.log(typeof(firstName)) //Symbol 22 | console.log(person) //{age:1} 23 | 24 | -------------------------------------------------------------------------------- /iterator/iterator.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by priya on 1/5/16. 3 | */ 4 | 5 | //example 1 6 | let i = [11,12,13]; 7 | i[3] = "value"; 8 | for (let iterate in i) { 9 | console.log(iterate); // output: 0 1 2 3 10 | } 11 | for (let iterate of i) { 12 | console.log(iterate); // output: 11 12 13 value 13 | } 14 | 15 | //example 2 16 | let values = [1, 2, 3]; 17 | let iterator = values[Symbol.iterator](); //using 'Symbol.iterator' 18 | console.log(iterator.next()); // "{ value: 1, done: false }" 19 | console.log(iterator.next()); // "{ value: 2, done: false }" 20 | console.log(iterator.next()); // "{ value: 3, done: false }" 21 | console.log(iterator.next()); //{value:undefined done:true} 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /scope/scope_function.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by priya on 28/4/16. 3 | */ 4 | { 5 | function alpha() { 6 | return 1; 7 | } 8 | 9 | console.log("aplha equal to 1", alpha() === 1) //true 10 | 11 | { 12 | function beta() { 13 | return 2; 14 | } 15 | 16 | console.log("aplha equal to 1", beta() === 2); //true 17 | console.log("aplha equal to 1", alpha() === 1); //false 'refer to inner function alpha' 18 | console.log("aplha equal to 3", alpha() === 3); //true 19 | function alpha() { 20 | return 3; 21 | } 22 | 23 | console.log("aplha equal to 3", alpha() === 3); //true 24 | 25 | 26 | } 27 | console.log("aplha equal to 1", alpha() === 1); //true 28 | //console.log("aplha equal to 1",beta()===2); //error beta is not defined 29 | } 30 | 31 | 32 | -------------------------------------------------------------------------------- /generators/generators.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by priya on 1/5/16. 3 | */ 4 | let createIterator = function *(items) { //generator function 5 | for (let i = 0; i < items.length; i++) { 6 | yield items[i]; 7 | console.log("After yield::",i); 8 | 9 | } 10 | }; 11 | 12 | let iterator = createIterator([1, 2, 3]); 13 | console.log(iterator.next()); // "{ value: 1, done: false }" 14 | console.log(iterator.next()); // "{ value: 2, done: false }" 15 | console.log(iterator.next()); // "{ value: 3, done: false }" 16 | console.log(iterator.next()); // "{ value: undefined, done: true }" 17 | 18 | 19 | /* 20 | Output: 21 | { value: 1, done: false } 22 | After yield:: 0 23 | { value: 2, done: false } 24 | After yield:: 1 25 | { value: 3, done: false } 26 | After yield:: 2 27 | { value: undefined, done: true } 28 | */ 29 | -------------------------------------------------------------------------------- /templating/template.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/29/16. 3 | */ 4 | 'use strict' 5 | var welcomeText = ''; 6 | var name = 'prince '; 7 | var surname = 'soni'; 8 | var location = 'noida'; 9 | var state = 'up'; 10 | var country = 'india'; 11 | 12 | welcomeText = 'Welcome ' + name + ' ' + surname + ' to ' + location + ' to \n' + state + 'to\n' + country; 13 | 14 | console.log(welcomeText) 15 | 16 | 17 | welcomeText = `Welcome ${name} ${surname} to ${location} 18 | to ${state} 19 | to ${country}`; 20 | console.log(welcomeText) 21 | 22 | 23 | welcomeText = `Welcome ${name} ${surname} to ${location} 24 | #### to ${state} 25 | to ${country}`; 26 | console.log(welcomeText) 27 | 28 | 29 | welcomeText = `Welcome ${name} ${surname} to ${location} 30 | to ${state} 31 | to ${country}`; 32 | console.log(welcomeText) 33 | -------------------------------------------------------------------------------- /misc/p2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/30/16. 3 | */ 4 | Number.EPSILON 5 | Number.isInteger(Infinity) // false 6 | Number.isNaN("NaN") // false 7 | 8 | Math.acosh(3) // 1.762747174039086 9 | Math.hypot(3, 4) // 5 10 | Math.imul(Math.pow(2, 32) - 1, Math.pow(2, 32) - 2) // 2 11 | 12 | "abcde".includes("cd") // true 13 | "abc".repeat(3) // "abcabcabc" 14 | 15 | Array.from(document.querySelectorAll('*')) // Returns a real Array 16 | Array.of(1, 2, 3) // Similar to new Array(...), but without special one-arg behavior 17 | [0, 0, 0].fill(7, 1) // [0,7,7] 18 | [1, 2, 3].find(x => x == 3) // 3 19 | [1, 2, 3].findIndex(x => x == 2) // 1 20 | [1, 2, 3, 4, 5].copyWithin(3, 0) // [1, 2, 3, 1, 2] 21 | ["a", "b", "c"].entries() // iterator [0, "a"], [1,"b"], [2,"c"] 22 | ["a", "b", "c"].keys() // iterator 0, 1, 2 23 | ["a", "b", "c"].values() // iterator "a", "b", "c" 24 | 25 | Object.assign(Point, { origin: new Point(0,0) }) 26 | -------------------------------------------------------------------------------- /class/p1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/30/16. 3 | */ 4 | 5 | 'use strict'; 6 | 7 | /*class Person { 8 | constructor(name) { 9 | this.name = name; 10 | } 11 | 12 | getName() { 13 | console.log("name of the person " , this.name); 14 | } 15 | 16 | setName(name) { 17 | this.name = name; 18 | } 19 | } 20 | var person = new Person('prince soni'); 21 | console.log('person.getName() ..... ',person.getName()); 22 | 23 | person.setName('akash yadav'); 24 | console.log('person.getName() ..... ',person.getName());*/ 25 | 26 | 27 | class Person { 28 | constructor(name) { 29 | this.name = name; 30 | } 31 | 32 | getName() { 33 | return this.name; 34 | } 35 | 36 | setName(name) { 37 | this.name = name; 38 | } 39 | } 40 | var person = new Person('prince soni'); 41 | 42 | console.log('person.getName() ..... ',person.getName()); 43 | 44 | person.setName('akash yadav'); 45 | 46 | console.log('person.getName() ..... ',person.getName()); 47 | -------------------------------------------------------------------------------- /generators/p2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/30/16. 3 | */ 4 | /** 5 | * Created by princesoni on 1/29/16. 6 | */ 7 | 8 | function* run(resume){ 9 | console.log("Generator started") 10 | var d1 = yield getData('/user1',resume); 11 | console.log("user 1", d1); 12 | var d2 = yield getData('/user2',resume); 13 | console.log("user 2"); 14 | var d3 = yield getData('/user3',resume); 15 | console.log("user 3"); 16 | var d4 = yield getData('/user4',resume); 17 | console.log("user 4"); 18 | } 19 | // var it = fetchResponse(); 20 | // console.log(it.next()) 21 | // console.log(it.next()) 22 | function getData(url, callback){ 23 | setTimeout(function(){ 24 | callback(url); 25 | },3000) 26 | } 27 | 28 | function run(gen){ 29 | var iterator, resume; 30 | 31 | resume = function(data){ 32 | 33 | var data1 = iterator.next(data); 34 | //console.log(data) 35 | } 36 | iterator = gen(resume); 37 | iterator.next(); 38 | } 39 | -------------------------------------------------------------------------------- /class/multi_level_inheritance.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by akash on 3/5/16. 3 | */ 4 | 5 | 6 | 7 | 8 | class Object{ 9 | constructor(id){ 10 | this.id=id; 11 | } 12 | func(){ 13 | console.log('id ... ',this.id); 14 | } 15 | 16 | func(){ 17 | console.log('id ... ',this.id); 18 | } 19 | } 20 | 21 | class Shape extends Object{ 22 | constructor(id,x,y){ 23 | super(id); 24 | this.x=x; 25 | this.y=y; 26 | // super(id); // error :: this is not defined 27 | } 28 | func(id,x,y){ 29 | super.func(); 30 | console.log('x ..... ',this.x,' ... y ......',this.y); 31 | //super.func();// working fine, but order is disturbed 32 | } 33 | } 34 | 35 | 36 | 37 | class Rectangle extends Shape { 38 | constructor (id, x, y, width, height) 39 | { 40 | 41 | super(id, x, y); 42 | 43 | this.width = width; 44 | this.height = height; 45 | } 46 | func(id,x,y,width,height){ 47 | super.func(); 48 | console.log('width .... ',this.width,' .. height ...... ',this.height); 49 | } 50 | } 51 | 52 | 53 | var obj=new Rectangle(1,2,3,4,5); 54 | obj.func(4,5,6,7,8); 55 | 56 | -------------------------------------------------------------------------------- /scope/scope_variable.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by priya on 28/4/16. 3 | */ 4 | 5 | //example 1 6 | let a=10; 7 | var b=20 8 | { 9 | let c=30; 10 | var d=40; 11 | console.log(a,b,c,d); //10 20 30 40 12 | a=50; 13 | b=60; 14 | console.log(a,b,c,d); //50 60 30 40 15 | } 16 | console.log(a,b,d); // 50 60 40 after changed 17 | //console.log(a,b,c,d); error : c is not defined 18 | 19 | 20 | //example 2 21 | var x1=10; 22 | var x1=20; //ok 23 | let b1=10; 24 | //let b1=20; error duplicate declaration 25 | b1=30; //ok 26 | 27 | 28 | 29 | //example 3 30 | let callbacks = []; 31 | for (let i = 0; i <= 2; i++) { 32 | callbacks[i] = function () { 33 | return i * 2; 34 | } 35 | } 36 | console.log(callbacks[0]() === 0); //true 37 | console.log(callbacks[1]() === 2); //true 38 | 39 | 40 | //if defined as var in loop 41 | /*let callbacks = []; 42 | for (var i = 0; i <= 2; i++) { 43 | callbacks[i] = function () { 44 | return i * 2; 45 | } 46 | } 47 | console.log(callbacks[0]() === 0); false 48 | console.log(callbacks[1]() === 2); false 49 | */ 50 | -------------------------------------------------------------------------------- /dataTypes/constants.js: -------------------------------------------------------------------------------- 1 | 2 | 'use strict' 3 | 4 | const a1=10; 5 | // const a=20; // error duplicate declaration 6 | { 7 | const a1=20; 8 | console.log(a,"In block"); //20 9 | } 10 | console.log(a,"In outer block") //10 11 | 12 | 13 | //const with objects 14 | const obj={ 15 | name:"priya" 16 | } 17 | /*const obj={ //duplicate declaration obj 18 | name:"priya", 19 | age:24 20 | }*/ 21 | 22 | obj.name='alpha' //allow to change the values of object 23 | const obj1={ 24 | name:"priya" 25 | } 26 | obj.age=24; //work fine 27 | //obj=obj1; // error obj is read-only only 28 | console.log(obj); //name: 'alpha', age:24; 29 | 30 | 31 | //const with arrays 32 | const arr=[1,2,3]; 33 | arr[1]=12; 34 | arr[3]=4; 35 | console.log("array",arr); 36 | //arr=[12,3]; //error arr is read-only property 37 | console.log("array changed", arr); 38 | 39 | 40 | //const with functions 41 | const aa=function(){ 42 | let def='klm'; 43 | console.log(def); 44 | } 45 | aa(); 46 | /*const aa=function(){ //duplicate declaration of a function 47 | 48 | }*/ 49 | 50 | //const with classes 51 | class alpha1{} 52 | const obj2=new alpha1(); 53 | var obj3=new alpha1(); 54 | //obj2=obj3;// error 55 | 56 | 57 | //use constants in classes 58 | const constant1 = 33, constant2 = 2; 59 | class Example { 60 | static get constant1() { 61 | return constant1; 62 | } 63 | static get constant2() { 64 | return constant2; 65 | } 66 | } 67 | 68 | const one = Example.constant1; 69 | const one1 = Example.constant2; 70 | console.log(one); //33 71 | console.log(one1); //2 72 | -------------------------------------------------------------------------------- /dataTypes/scope.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by princesoni on 1/29/16. 3 | */ 4 | 'use strict' 5 | let x = 0; 6 | 7 | console.log(x) 8 | 9 | if(true){ 10 | let y =100 11 | } 12 | //Eror y is not defined 13 | //console.log(y) 14 | 15 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++// 16 | 17 | 18 | let callbacks = [] 19 | for (let i = 0; i <= 2; i++) { 20 | callbacks[i] = function () { return i * 2 } 21 | } 22 | console.log(callbacks[0]() === 0) 23 | console.log(callbacks[1]() === 2) 24 | console.log(callbacks[2]() === 4) 25 | 26 | 27 | 28 | /* 29 | let callbacks = [] 30 | for (var i = 0; i <= 2; i++) { 31 | callbacks[i] = function(i){ return i * 2 }(i) 32 | } 33 | console.log(callbacks[0] === 0) 34 | console.log(callbacks[1] === 2) 35 | console.log(callbacks[2] === 4) 36 | */ 37 | 38 | 39 | const ARRAY = [1,2,3,4,5]; 40 | 41 | const ARRAY2 = [5,6,7,8,9,0]; 42 | 43 | 44 | 45 | for (let i = 0; i < ARRAY.length; i++) { 46 | const x = ARRAY[i] 47 | } 48 | 49 | //console.log('x ... ',x);// error x is not defined 50 | 51 | for (let i = 0; i < ARRAY2.length; i++) { 52 | const y = ARRAY2[i] 53 | } 54 | 55 | //console.log('y ... ',y); // error y is not defined 56 | 57 | 58 | console.log('i ....',i); 59 | 60 | var i =9; 61 | 62 | console.log('i ....',i); 63 | 64 | 65 | 66 | { 67 | { 68 | const constant1 = 33, 69 | constant2 = 2; 70 | } 71 | 72 | class Example { 73 | 74 | static get constant1() { 75 | return constant1; 76 | } 77 | 78 | static get constant2() { 79 | return constant2; 80 | } 81 | } 82 | 83 | /* const one = Example.constant1;//error 84 | 85 | console.log('..... one ....',one);*/ 86 | 87 | } 88 | 89 | var list = [ 1, 2, 3 ]; 90 | 91 | var [ab, ,ba] = list; 92 | 93 | [ ba, ab ] = [ ab, ba ]; 94 | 95 | console.log('a1 ..... ',ab); 96 | 97 | console.log('b1 ..... ',ba); 98 | 99 | //console.log('list new ..... ',[ a1, b1 ]); 100 | 101 | 102 | 103 | { 104 | let abc =13; 105 | } 106 | var abc = 23; 107 | 108 | -------------------------------------------------------------------------------- /destructing/destructuring.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by priya on 28/4/16. 3 | */ 4 | //example 1 5 | var x=10; 6 | var y=20; 7 | var obj = { x, y } 8 | console.log(obj.x); //10 9 | console.log(obj.y); //20 10 | 11 | //example 2 12 | function a(){ 13 | return 1; 14 | } 15 | var obj2={ 16 | foo: "bar", 17 | ['b'+a()]:"dynamic" 18 | } 19 | console.log(obj2.foo); //bar 20 | console.log(obj2.b1); //dynamic 21 | 22 | //swapping of two numbers 23 | var list1 = [ 1, 2, 3 ]; 24 | var [ a1, , b ] = list1; 25 | [ b, a1 ] = [ a1, b ]; 26 | console.log("a:",a1); //3 27 | console.log("b:",b); //1 28 | console.log(list1); //[1,2,3] 29 | 30 | //object matching shorthand notation 31 | function getASTNode(){ 32 | return { 33 | op:21, 34 | lhs:{op:22}, 35 | rhs:23 36 | } 37 | } 38 | var { op,lhs, rhs } = getASTNode(); 39 | console.log(op,lhs.op, rhs); //21,22,23 40 | 41 | var obj=getASTNode(); 42 | console.log(obj.op,obj.lhs.op, obj.rhs); //21,22,23 43 | 44 | //parameter context matching 45 | function f ([ name, val ]) { 46 | console.log(name, val); 47 | } 48 | function g ({ name: n, val: v }) { 49 | console.log(n, v); 50 | } 51 | function h ({ name, val }) { 52 | console.log("in function") 53 | console.log(name, val); 54 | } 55 | f([ "In f", 42 ]); 56 | g({ name: "In g", val: 7 }); 57 | h({ name: "In h", val: 42 }) 58 | //f({ name: "In h", val: 42 }) output: TypeError: Invalid attempt to destructure non-iterable instance 59 | //h(["in j", 32]); output: [undefined undefined] function is called but values are not assigned to the paramters; 60 | 61 | 62 | // put array elements into variables 63 | var list = [ 7, 42 ]; 64 | var [ a , b = 2, c = 3, d ] = list; 65 | console.log(a === 7); //true 66 | console.log(b === 42); //true 67 | console.log(c === 3); //true 68 | console.log(d === undefined); //true 69 | 70 | 71 | var l=[7]; 72 | var [a,b=2,c]=l; 73 | console.log(a, b, c); //7, 2, undefined 74 | 75 | 76 | //Problem with destructurng of arrays.. 77 | let colors = [ "red", [ "green", "lightgreen" ], "blue" ]; 78 | 79 | let [ firstColor, [ secondColor ] , third] = colors; 80 | 81 | console.log(firstColor); // red 82 | console.log([secondColor]); //["green"] 83 | console.log(secondColor); //green 84 | console.log(third); //blue 85 | -------------------------------------------------------------------------------- /dataTypes/constUse2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by akash on 28/4/16. 3 | */ 4 | 5 | 'use strict' 6 | 7 | 8 | const CONST1 = 'akash'; 9 | const PI = 3.14159; 10 | 11 | console.log('CONST1 : ',CONST1,' PI : ',PI); 12 | 13 | /*CONST1 = 'yadav';//error Assignment to constant variable. 14 | PI=3;//error Assignment to constant variable. 15 | 16 | console.log('CONST1 : ',CONST1,' PI : ',PI);*/ 17 | 18 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++// 19 | 20 | 21 | const VARIABLE= {a:'asd',b:13}; 22 | 23 | const VARIABLE2 = {c:'asdasd',b:1233}; 24 | 25 | VARIABLE.a = 'qaz';// no error 26 | 27 | console.log(VARIABLE);//{ a: 'qaz', b: 13 } 28 | 29 | VARIABLE.c = 'error';// no error 30 | 31 | console.log(VARIABLE);//{ a: 'qaz', b: 13, c: 'error' } 32 | 33 | //VARIABLE = {a:'asd',b:13, d : 234};//error Assignment to constant variable. 34 | 35 | //console.log(VARIABLE); 36 | 37 | //VARIABLE = VARIABLE2//error Assignment to constant variable. 38 | 39 | //console.log(VARIABLE); 40 | 41 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++// 42 | 43 | 44 | const ARRAY = [1,2,3,4,5]; 45 | 46 | const ARRAY2 = [5,6,7,8,9,0]; 47 | 48 | ARRAY[0] = 11;// no error 49 | 50 | console.log(ARRAY);//[ 11, 2, 3, 4, 5 ] 51 | 52 | ARRAY[5] = 66;// no error 53 | 54 | console.log(ARRAY);//[ 11, 2, 3, 4, 5, 66 ] 55 | 56 | //ARRAY = [9,8,7,6,5,4];//error Assignment to constant variable. 57 | 58 | //console.log(ARRAY); 59 | 60 | //ARRAY = ARRAY2//error Assignment to constant variable. 61 | 62 | //console.log(ARRAY); 63 | 64 | 65 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++// 66 | 67 | const FUN1 = function(){ 68 | console.log('FUN1'); 69 | } 70 | var FUN2 = function(){ // const FUN2 = function(){ 71 | console.log('FUN2'); 72 | } 73 | 74 | FUN1(); 75 | 76 | FUN2(); 77 | 78 | //FUN1 = FUN2 ;//error Assignment to constant variable. 79 | 80 | 81 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++// 82 | 83 | 84 | const CLASS_A = class { 85 | 86 | fun3(){ 87 | console.log("class a fun3 ..."); 88 | } 89 | fun3(){ 90 | console.log("class a fun3 ...");// override 91 | } 92 | 93 | } 94 | 95 | var obj = new CLASS_A(); 96 | 97 | obj.fun3(); 98 | 99 | 100 | class class_b extends CLASS_A{ 101 | fun4(){ 102 | console.log("call via fun4 .... "); 103 | super.fun3(); 104 | } 105 | } 106 | 107 | 108 | /*const CLASS_A = class // error Duplicate declaration "CLASS_A" 109 | { 110 | fun5() 111 | { 112 | console.log("call via fun5 .... "); 113 | } 114 | } 115 | 116 | var objA = new CLASS_A(); 117 | objA.fun5();*/ 118 | 119 | var obj2 = new class_b(); 120 | 121 | obj2.fun4(); 122 | 123 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++// 124 | 125 | for (let i = 0; i < ARRAY.length; i++) { 126 | const x = ARRAY[i] 127 | } 128 | 129 | //console.log('x ... ',x);// error x is not defined 130 | 131 | --------------------------------------------------------------------------------