From bbc857ac6540f0e02bef9e8567dc6ad20e1eb2e3 Mon Sep 17 00:00:00 2001 From: Aman Singh Date: Fri, 26 Jul 2019 17:46:16 +0530 Subject: [PATCH 01/10] Week -4 tasks --- Week-4/ES6/Symbols/symbols.challenge.js | 39 +++++++ Week-4/ES6/Symbols/symbols.example.js | 67 +++++++++++ .../iterators & generators/generators.html | 17 +++ .../iterators_generators_example.js | 12 ++ Week-4/ES6/readme.md | 0 .../static methods/staticmethods.example.js | 54 +++++++++ Week-4/ES6/subclass/subclass.challenge.js | 66 +++++++++++ Week-4/ES6/subclass/subclass.example.js | 37 ++++++ Week-4/ES6/tagged_templates/index.html | 6 + .../tagged_template.challenge.js | 23 ++++ .../tagged_templates.example.js | 73 ++++++++++++ .../template_literals.challenge.js | 16 +++ .../template_literals.example.js | 41 +++++++ .../Exponentiation/Exponentiation.example.js | 61 ++++++++++ Week-4/ES7/readme.md | 7 ++ .../proxy_constructor.challenge.js | 46 ++++++++ .../proxy_constructor.example.js | 107 ++++++++++++++++++ Week-4/ES8/Reflect/reflect.challenge.js | 9 ++ Week-4/ES8/Reflect/reflect.example.js | 79 +++++++++++++ .../rest_spread_example.js | 69 +++++++++++ 20 files changed, 829 insertions(+) create mode 100644 Week-4/ES6/Symbols/symbols.challenge.js create mode 100644 Week-4/ES6/Symbols/symbols.example.js create mode 100644 Week-4/ES6/iterators & generators/generators.html create mode 100644 Week-4/ES6/iterators & generators/iterators_generators_example.js create mode 100644 Week-4/ES6/readme.md create mode 100644 Week-4/ES6/static methods/staticmethods.example.js create mode 100644 Week-4/ES6/subclass/subclass.challenge.js create mode 100644 Week-4/ES6/subclass/subclass.example.js create mode 100644 Week-4/ES6/tagged_templates/index.html create mode 100644 Week-4/ES6/tagged_templates/tagged_template.challenge.js create mode 100644 Week-4/ES6/tagged_templates/tagged_templates.example.js create mode 100644 Week-4/ES6/template literals/template_literals.challenge.js create mode 100644 Week-4/ES6/template literals/template_literals.example.js create mode 100644 Week-4/ES7/Exponentiation/Exponentiation.example.js create mode 100644 Week-4/ES7/readme.md create mode 100644 Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js create mode 100644 Week-4/ES8/Proxy_Constructors/proxy_constructor.example.js create mode 100644 Week-4/ES8/Reflect/reflect.challenge.js create mode 100644 Week-4/ES8/Reflect/reflect.example.js create mode 100644 Week-4/ES9/rest & spread for objects/rest_spread_example.js diff --git a/Week-4/ES6/Symbols/symbols.challenge.js b/Week-4/ES6/Symbols/symbols.challenge.js new file mode 100644 index 0000000..98168ab --- /dev/null +++ b/Week-4/ES6/Symbols/symbols.challenge.js @@ -0,0 +1,39 @@ +const CARCOLOR = Symbol(); // give red and no see changes in below console +const CARMAKE = Symbol(); +const CARMODEL = Symbol(); + +class Car { + constructor(color, make, model) { + this[CARCOLOR] = color; + this[CARMAKE] = make; + this[CARMODEL] = model; + } + set color(color){ + this[CARCOLOR] = color; + } + set model(model){ + this[CARMODEL] = model; + } + set make(make){ + this[CARMAKE] = make; + } + get color() { + return this[CARCOLOR]; + } + get model() { + return this[CARMODEL]; + } + get make() { + return this[CARMAKE]; + } +} +let myCar = new Car('Red', 'Porsche', 'Cayanne'); +console.log(myCar); +myCar.color = `black`; +myCar.make = `Jaguar`; +myCar.model = `Land Rover`; + +console.log(myCar); + +// implement the functionality to set and get values to car color - Black, car model - Land Rover and car maker - Jaguar +// you should assign values to those private variables inside class - use setter and getter methods diff --git a/Week-4/ES6/Symbols/symbols.example.js b/Week-4/ES6/Symbols/symbols.example.js new file mode 100644 index 0000000..b83969d --- /dev/null +++ b/Week-4/ES6/Symbols/symbols.example.js @@ -0,0 +1,67 @@ +// console.log(typeof(true)); +// console.log(typeof(null)); +// console.log(typeof(undefined)); +// console.log(typeof(2)); +// console.log(typeof(`a`)); +// console.log(typeof({})); + +// new Datatype - symbols - a primitive datatype - a SYMBOL is something which is an identifier, +// symbols provide totally unique something that we can assign to variables, symbol is not a constructor, +// we can not so new Symbol(), it's just like a string + +// const string1 = String('Hello'); +// const string2 = "Hello"; + +// console.log(string1); +// console.log(string2); + +// const aSymbol = Symbol(); +// console.log(aSymbol); + +console.log(Symbol() === Symbol()) + +// symbols are used to avoid name collisions in properties of an object +// we can't guarantee normally with property names specifically in objects +// or more specifically in constructors and classes that a property name is actually private that it can't get messed with + +const a = Symbol(); +const b = Symbol(); + +console.log(a===b); // it yields false because they both reference different w=even if they look same and alike +// because we are comparing a random value with another random, symbols will guarantee that one Symbol can never get get used again + +/* +class Car { + constructor (color, make, model) { + this.color = color; + this.make = make; + this.model = model; + } +} + +let myCar = new Car('Red', 'Porsche', 'Cayanne'); +console.log(myCar); +myCar.color = `blue`; +console.log(myCar); +*/ + +const CARCOLOR = Symbol(); // give red and no see changes in below console +const CARMAKE = Symbol(); +const CARMODEL = Symbol(); + +// console.log(CARCOLOR, CARMAKE, CARMODEL) + +class Car { + constructor(color, make, model) { + this[CARCOLOR] = color; + this[CARMAKE] = make; + this[CARMODEL] = model; + } +} + +let myCar = new Car('Red', 'Porsche', 'Cayanne'); +console.log(myCar); +myCar.color = `blue`; +console.log(myCar.color); +// console.log(myCar); +// console.log(Symbol.for(`test`) === Symbol.for(`test`)) \ No newline at end of file diff --git a/Week-4/ES6/iterators & generators/generators.html b/Week-4/ES6/iterators & generators/generators.html new file mode 100644 index 0000000..b36d33f --- /dev/null +++ b/Week-4/ES6/iterators & generators/generators.html @@ -0,0 +1,17 @@ + + + \ No newline at end of file diff --git a/Week-4/ES6/iterators & generators/iterators_generators_example.js b/Week-4/ES6/iterators & generators/iterators_generators_example.js new file mode 100644 index 0000000..22dd5c3 --- /dev/null +++ b/Week-4/ES6/iterators & generators/iterators_generators_example.js @@ -0,0 +1,12 @@ +function* aGenerator(){ + for(let i=0;i<10;i++){ + console.log("I just ran"); + yield ; //PAUSE HERE + console.log("I ran too"); + } +} +const gen = aGenerator(); +console.log(gen.next('aman')); +console.log(gen.next()); +console.log(gen.next()); +console.log(gen.next()); diff --git a/Week-4/ES6/readme.md b/Week-4/ES6/readme.md new file mode 100644 index 0000000..e69de29 diff --git a/Week-4/ES6/static methods/staticmethods.example.js b/Week-4/ES6/static methods/staticmethods.example.js new file mode 100644 index 0000000..e648564 --- /dev/null +++ b/Week-4/ES6/static methods/staticmethods.example.js @@ -0,0 +1,54 @@ +// function superhero(name, strength, speed) { +// this.name = name; +// this.strength = strength; +// this.speed = speed; +// } +// superhero.prototype.goodHero = true; +// superhero.prototype.powerUp = function(){ +// this.strength +=5; +// } +// let hero1 = new superhero("Hulk", 10, 5); +// console.log(hero1); +// hero1.powerUp(); +// console.log(hero1); + +//ES6 implementation + +class superhero { + constructor(name, strength, speed) { + this._name = name; + this._strength = strength; + this._speed = speed; + this.goodHero = true; // focus on this + } + powerUp(){ + this._strength += 5; + } + get name(){ + console.log(" name"+this._name); + } + set name(newname){ + this._name = newname; + } + static goodHero(){ + return true; + } +} + +const hulkdetails = ["Hulk", 10, 5]; +const bulkdetails = ["Bulk", 10, 5]; + +let hero1 = new superhero(...hulkdetails); +// console.log(hero1.name); +// console.log(hero1) +// +// hero1.powerUp(); +// hero1.powerUp(); +// hero1.powerUp(); +// console.log(hero1) + +let hero2 = new superhero(...bulkdetails); +console.log(superhero.goodHero()) + +// hero1.name = "george"; +console.log(hero1) diff --git a/Week-4/ES6/subclass/subclass.challenge.js b/Week-4/ES6/subclass/subclass.challenge.js new file mode 100644 index 0000000..5a62158 --- /dev/null +++ b/Week-4/ES6/subclass/subclass.challenge.js @@ -0,0 +1,66 @@ + +// inheritance - subclasses + +//implement methods in subclass (Human) which will override parent class functionality + +//1. implement a custom function which will add "Mr" to name property in subclass +//2. implement a method in subclass which will increment the power by 2 +//3. implement a method in subclass to reduce the power by half +//4. add 2 more properties to Human class - city and state (private to human class) +class superhero { + constructor(name, strength, speed) { + this._name = name; + this._strength = strength; + this._speed = speed; + // this.goodHero = true; // focus on this + } + powerUp() { + this.strength += 5; + } + get name() { + console.log("get name"); + return this._name; + } + set name(newname) { + this._name = newname; + } + static goodHero() { + return true; + } +} + +class Human extends superhero { + constructor(healthpoints,city,state,...superherostuff) { + super(...superherostuff); + this._healthpoints = healthpoints; + this.city=city; + this.state=state; + + } + newName() { + this._name=`Mr ${this._name}`; + } + powerHalf() { + this._strength /= 2; + } + powerDouble() { + this._strength *= 2; + } +} +function newName() { + this._name=`Mr ${this._name}`; + + } +const details = [20,"unnao","uttar pradesh","Bill", 10, 8]; +const hero3 = new Human(...details); +console.log(hero3); +hero3.newName(); +hero3.powerHalf(); +console.log(hero3); + + + +// hero3.name = "sai"; +// console.log(hero3) +// const hero4 = new superhero(20, "abc", 8); +// console.log(hero4); diff --git a/Week-4/ES6/subclass/subclass.example.js b/Week-4/ES6/subclass/subclass.example.js new file mode 100644 index 0000000..eba2fd9 --- /dev/null +++ b/Week-4/ES6/subclass/subclass.example.js @@ -0,0 +1,37 @@ +// inheritance - subclasses +class superhero { + constructor(name, strength, speed) { + this._name = name; + this._strength = strength; + this._speed = speed; + // this.goodHero = true; // focus on this + } + powerUp() { + this.strength += 5; + } + get name() { + console.log("get name"); + return this._name; + } + set name(newname) { + this._name = newname; + } + static goodHero() { + return true; + } +} + +class Human extends superhero{ + constructor(healthpoints, ...superherostuff){ + super(...superherostuff); + this._healthpoints = healthpoints; + } + +} +const details = [20,"Bill",10,8] +const hero3 = new Human(...details); +console.log(hero3); +hero3.name = "sai"; +console.log(hero3) +const hero4 = new superhero(20,"abc", 8); +console.log(hero4); diff --git a/Week-4/ES6/tagged_templates/index.html b/Week-4/ES6/tagged_templates/index.html new file mode 100644 index 0000000..69db53c --- /dev/null +++ b/Week-4/ES6/tagged_templates/index.html @@ -0,0 +1,6 @@ + + +
+ + + \ No newline at end of file diff --git a/Week-4/ES6/tagged_templates/tagged_template.challenge.js b/Week-4/ES6/tagged_templates/tagged_template.challenge.js new file mode 100644 index 0000000..075e310 --- /dev/null +++ b/Week-4/ES6/tagged_templates/tagged_template.challenge.js @@ -0,0 +1,23 @@ +//With the template provided, write a template tag that will validate an SQL statement: +//Only SELECT or UPDATE statements are valid - write validation logic which allows only SELECT, UPDATE +//The passwords table cannot be altered +//there is no order by clause, add it and order by asc +// When done, return the reconstructed query or a note any errors + +let city = `Chicago` //data we got from somewhere else (api or database) +let userId = 3; //data we got from somewhere else (api or database) +let command = `SELECT *`; +let table = `USERS`; +let whereClauses = [ + `uid = ${2+1}`, + `OR city = ${city}` +] + +// Your code to call the tag and log the return value here... +// your output should be - SELECT * FROM USERS WHERE uid = 3 OR city = chicago ORDER BY asc +// Spread syntax is a more common and easy here if you are comfortable with it +function checkQuery() { + //Your validation code here... + + return (finalQuery); +} diff --git a/Week-4/ES6/tagged_templates/tagged_templates.example.js b/Week-4/ES6/tagged_templates/tagged_templates.example.js new file mode 100644 index 0000000..a0f6826 --- /dev/null +++ b/Week-4/ES6/tagged_templates/tagged_templates.example.js @@ -0,0 +1,73 @@ +let line1 = "we are moving onto tagged templates, tagged templates are really popular but incredibly under served"; +let line2 = "there is less documentation available for this, initially we aren't sure why should we use it "; +let line3 = "once we got it down, it actually it is quite powerful, the use-cases are very limited"; + +let lines = [line1, line2, line3]; + +//Tagged templates +function buildHTML(param){ + console.log("it works", param); +} + +const result = buildHTML(3); +// console.log(result); + +const result2 = buildHTML`what's going on here`; +// console.log(result2); + +function build_HTML(strings, expressions) { + console.log(strings); + console.log(expressions); +} + + +// const result3 = build_HTML`
  • ${lines}
  • `; +// const result4 = build_HTML `
  • ${lines[0]}
  • `; + +// function build_HTML_new(strings, expression1, expression2) { +// console.log(strings); +// console.log(expression1); +// console.log(expression2); +// } +// const result5 = build_HTML_new `
  • ${lines[0]}${lines[1]}
  • `; + +function build_HTML_map(tags, lines) { + console.log(tags); + console.log(lines); + const newHtml = lines.map(function(line){ + return `${tags[0]}${line}${tags[1]}` + }); + return newHtml; +} +const result6 = build_HTML_map`
  • ${lines}
  • `; +console.log(result6); +// document.querySelector('#quotes').innerHTML = result6; + +// const output = buildHTML2`
  • ${lines}
  • `; +// output('#quotes'); +// function buildHTML2(tags, lines) { +// return function(element) { +// const newHTML = lines.map(function(line){ +// return `${tags[0]}${line}${tags[1]}` +// }); +// const finalHTML = newHTML.join(''); +// document.querySelector(element).innerHTML += finalHTML; +// } +// } + + + +function testFunction(){ + return "hello, from Refinitiv"; +} +const templateLiteralCallback1 = `Rob, ${() => testFunction()}`; +console.log(templateLiteralCallback1); + +const templateLiteralCallback2 = `Rob, ${testFunction()}`; +console.log(templateLiteralCallback2); + +function taggedcallback(strings, func) { + return strings[0] + func(); +} +const taggedLiteralcallback = taggedcallback`Rob, ${() => testFunction()}`; +console.log(taggedLiteralcallback); diff --git a/Week-4/ES6/template literals/template_literals.challenge.js b/Week-4/ES6/template literals/template_literals.challenge.js new file mode 100644 index 0000000..3ca5251 --- /dev/null +++ b/Week-4/ES6/template literals/template_literals.challenge.js @@ -0,0 +1,16 @@ +// using template literals build a JS program to check if a person is eligible for a price discount or not +// check if +// if a registered member - discount is 5% +// if not registered member - discount is 4% +const users=['aman','vijay','bharath','gagan']; +function registeredMember(name){ + if(users.indexOf(name) !=-1){ + discount=5; + } + else{ + discount=4; + } + return `discount is ${discount}%` +} +console.log(registeredMember('aman')); +console.log(registeredMember('xyz')); diff --git a/Week-4/ES6/template literals/template_literals.example.js b/Week-4/ES6/template literals/template_literals.example.js new file mode 100644 index 0000000..fed61a5 --- /dev/null +++ b/Week-4/ES6/template literals/template_literals.example.js @@ -0,0 +1,41 @@ +const name = "Rob"; +const hello = "hello" + name; +console.log(hello); + +const selectQuery = 'SELECT * FROM users WHERE name ="' + name + '" AND city = "' + name + '"'; +console.log(selectQuery); +//use Graveney ``, interchangable with '' and "", it gives the string some more power +const tryit = `hello, ${name}`; +console.log(tryit); + +const name1 = "Javascript"; +const city = "Hyderabad"; + +const selectQuery1 = `SELECT * from users WHERE name = "${name1}" AND city = "${city}"`; +console.log(selectQuery1); + +//Multi line feature +// const string = "Yesterday was a rainy day, +// lot of traffic" +const string1 = "Yesterday was a rainy day,"+ +"lot of traffic"; +console.log(string1); + +const string2 = "Yesterday was a rainy day, \n" + + "lot of traffic"; +console.log(string2); + +const string3 = `Yesterday was a rainy day, + lot of traffic`; +console.log(string3); + +const newHTML = ` +
    +${name} +
    ` + +console.log(newHTML); + +//Expressions +const anExpression = `43 ** 23 = ${43*23}`; +console.log(anExpression); diff --git a/Week-4/ES7/Exponentiation/Exponentiation.example.js b/Week-4/ES7/Exponentiation/Exponentiation.example.js new file mode 100644 index 0000000..1cc131f --- /dev/null +++ b/Week-4/ES7/Exponentiation/Exponentiation.example.js @@ -0,0 +1,61 @@ +const x = 2 * 2 * 2 * 2 * 2; +console.log(x); + +const x2 = 2 ** 5; + +const a = 3 ** 2.5; + +const b = 100* -1; + +const d = 2 ** 3 ** 4; + +const e = (2 ** 3) ** 4; +const f = 2 ** (3 ** 4); +console.log(d , e ,f); + + +// includes is essentially same as indexof except it returns a boolean + +// const arr = [1,2,3,4,5]; +// if(arr.indexOf(22)> -1) { +// console.log("found"); +// } else { +// console.log("not found"); +// } + +// const inArr = arr.includes(2); +// console.log(inArr); + +//indexof uses strict comparision, compares with every single value internally + +console.log(NaN === NaN) + +const arr = [1, 2, 3, 4, 5, NaN]; +if (arr.indexOf(NaN) > -1) { + console.log("found"); +} else { + console.log("not found"); +} + +const inArr = arr.includes(NaN); +console.log(inArr); + +// destructuring rest paramaters + +const someJSON = { + name: "Wayne Rooney", + position: "Forward", + club: "Manchester United" +} + +const {name, position, club} = someJSON; + +console.log(name, position, club); + +function sum(...array) { + return array.reduce((number, total) => { + return number + total; + }) +} + +console.log(sum(1,4,7,3)); diff --git a/Week-4/ES7/readme.md b/Week-4/ES7/readme.md new file mode 100644 index 0000000..2dac7aa --- /dev/null +++ b/Week-4/ES7/readme.md @@ -0,0 +1,7 @@ +3 new additions + exponentiation operator + includes methods for arrays and typed arrays + destructure for rest parameters +2 deletions + removed enumerate from proxy handler + generators and their methods are not constructable anymore \ No newline at end of file diff --git a/Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js b/Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js new file mode 100644 index 0000000..05d273e --- /dev/null +++ b/Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js @@ -0,0 +1,46 @@ +// 1. SET +// Rewrite the sample code below so that every time a property is set a callback runs. +// your callback will check if the property changed is employees. + +//If it is employees, make sure if it is an Array, a string or null. +//If anything else comes, respond with an appropriate message +//If not employees, let it pass + +let manager = { + office: `Dubai`, + dept: `sales`, + employees: 0 +} + +manager.office = `London` //updates +manager.employees = ['Jim', 'Patrick', 'Mary']; //updates +manager.employees = 3; // doesn't update +manager.employees = null; // updates +manager.employees = {name:'Jim'} // doesn't update + +// 2. GET +// adjust the following code so that anytime an internal object with accessLevel of 1 is accessed, +//"Access Denied" is returned. + +const users = [ + { + username: `bob`, + accessLevel: 1, + accessCode: 1234 + }, + { + username: `Mary`, + accessLevel: 2, + accessCode: 2345 + }, + { + username: `Harold`, + accessLevel: 2, + accessCode: 9999 + } +] + +console.log(users[0].username) // Access Denied +console.log(users[0].accessCode) // Access Denied +console.log(users[1].accessCode) // 2345 +console.log(users[2].username) // Harold \ No newline at end of file diff --git a/Week-4/ES8/Proxy_Constructors/proxy_constructor.example.js b/Week-4/ES8/Proxy_Constructors/proxy_constructor.example.js new file mode 100644 index 0000000..88a3262 --- /dev/null +++ b/Week-4/ES8/Proxy_Constructors/proxy_constructor.example.js @@ -0,0 +1,107 @@ +// proxy constructor and can be invoked with new - allows us to re-architecture our code +// proxy in english is a middle man +// ex: Microsoft stock +// proxy constructor allows us to cut off almost any part of object change process +// this is new because all of this stuff happens internally and we can't see it by any hooks +// and hence we are creating a middleware (traps!!)- ex: express.js +// using ths we can hijack get, set processes - intercept +// not same as setters and getters in class system, they work little bit different +// API exposes get set has construct etc - total 13 methods +// getPropertyOf() +// setPropertyOf() +// isExtensible() +// preventExtensions() +// getOwnPropertyDescriptor() +// defineProperty() +// deleteProperty() +// has() +// get() +// set() +// ownKeys() +// apply() +// construct() + +// tis gives exclusive control over objects - to get and set +// validations +// logic / Middleware + +//proxy is a constructor and takes 2 args +// 1. Object to proxy and 2. Object serving as the handler + +let handler = { + // this has all the above properties to trap + // get trap // takes 2 args - target object and the property asked for + get: (target, propName) => { + // console.log(target, propName); + //return undefined; + return target[propName]; + }, + // set takes 3 args - target obj, property and new val of property + set: (target, propName, newVal) => { + console.log(target, propName, newVal); + target[propName] = newVal; + }, + // has: (target, propName) => { + // // we can use reflex here - pending for next + // console.log("check"); + // } +} + +let newObj = new Proxy({}, handler); +newObj.name = "rob"; +newObj.job = "Instructor"; +console.log(newObj.name); // WHAT'S GOING ON HERE?? WHY UNDEFINED?? +console.log(newObj.job); +newObj.age = 99; +console.log(newObj.age); // change the get-return part to send something for age property too +console.log(newObj); + +// if("name" in newObj) { +// console.log("I found it!!"); +// } + +// restrict user to set non-numeric values for age +// check if the property is existed in object + + +// ---------- another example + +// class Car { +// constructor(make, model) { +// this.make = make; +// this.model = model; +// } +// printInfo() { +// console.log(this.make, this.model); +// } +// } + +// let handler1 = { +// get: (target, propName) => { +// console.log(`someone is trying to access ${propName} property`) +// } +// } + +// let myCar = new Car(`toyota`, `camry`); +// let carProxy = new Proxy(myCar, handler1); +// console.log(carProxy.make); + + +// --------------------- another example --------------- + +function sum (x,y){ + return x+y; +} + +let handler2 = { + //apply trap takes 3 args - target, the this, argumentlist + apply: (target, thisArg, argsList) => { + console.log("someone called this function"); + // return target(argsList[0], argsList[1]) * 100 + } +} + +const sumProxy = new Proxy(sum, handler2); + +console.log(sum(2,3)); +console.log(sumProxy(2, 3)); \ No newline at end of file diff --git a/Week-4/ES8/Reflect/reflect.challenge.js b/Week-4/ES8/Reflect/reflect.challenge.js new file mode 100644 index 0000000..7f0185f --- /dev/null +++ b/Week-4/ES8/Reflect/reflect.challenge.js @@ -0,0 +1,9 @@ + +let course = { + +} + +// use reflect API to define property, delete a property, getOwnpropertydescriptor details +// property to define - name with value as "Javascript", make it read only (writable : false) +// property to define - duration with value as "3 hours", make it read only (writable : false) +// getOwnpropertydescriptor - check and print metadata about name property diff --git a/Week-4/ES8/Reflect/reflect.example.js b/Week-4/ES8/Reflect/reflect.example.js new file mode 100644 index 0000000..ceb465d --- /dev/null +++ b/Week-4/ES8/Reflect/reflect.example.js @@ -0,0 +1,79 @@ +// Reflection is to examine, introspect or modify your program at run-time +// Reflect and proxy both go hand in hand +// Reflect can't be instantiated and invoked +// Reflect is a standalone global Object than has methods that are 1:1 with proxy handlers + +// Standalone global Object - Math() - math is a static object with whole bunch of static methods +// static methods - they wnt attached to the instance but they are attached to class + +// console.log(new Reflect()); + +// Reflect.get() takes 2 properties +// 1. target, 2.property + +const x = { + a:1, b:2 +} + +console.log(Reflect.get(x, `a`)); +console.log(x[`a`]); + +const arr = ["joe", "akash", "mary"]; +console.log(arr[0]) +console.log(Reflect.get(arr,0)); + + +const monster1 = { + secret: 'secret', + counter: 4 +}; + +const handler1 = { + get: function (target, prop, receiver) { + if (prop === 'secret') { + return `You can't access secret`; + } else { + //console.log(Reflect.get(target, prop, receiver)) + // arguments[0][prop] + return Reflect.get(...arguments); + //fast and easy way, the win with reflect here is very explicit + //and we are accessing something inside a variable + } + } +}; + +const proxy1 = new Proxy(monster1, handler1); + +console.log(proxy1.counter); +// expected output: 4 + +console.log(proxy1.secret); + +// ---------------- reflect.has +// works as hasownproperty and in + +const obj = { + number : 22 +} + +console.log(obj.hasOwnProperty('number')); +console.log('number' in obj); +console.log(Reflect.has(obj, 'number')); + +// ---------------reflect.apply +function sum(...array) { + return array.reduce((number, total)=> { + return number + total; + }) +} + +// sum.apply = function(){ +// throw new Error('I broke apply function!'); +// } + +console.log(sum[1,2,3]); +console.log(sum.apply(null, [1,2,3])); +console.log(Function.apply.call(sum, null, [1,2,3])); +console.log(Reflect.apply(sum, null, [1, 2, 3])); + +// if proxy provides traps to change objects, Reflect provides introspection to get data about objects \ No newline at end of file diff --git a/Week-4/ES9/rest & spread for objects/rest_spread_example.js b/Week-4/ES9/rest & spread for objects/rest_spread_example.js new file mode 100644 index 0000000..65ebf21 --- /dev/null +++ b/Week-4/ES9/rest & spread for objects/rest_spread_example.js @@ -0,0 +1,69 @@ +const user = { + firstName: "Nagasai", + lastName: "P", + session: "Javascript", + city: "Hyderabad", + state: "telangana" +} + +//how to create a new object without city property + +const {...newuser} = user; +console.log(newuser); +delete newuser.city +console.log(newuser); + +// const {firstName, lastName, session, state} = user +// console.log(firstName); + +// const {city, ...newuser2} = user; +// console.log(newuser2); + +// const { +// city, +// state, +// ...newuser3 +// } = user; +// console.log(newuser3); + +// ----------------- Regular Expressions + +// const theMatch = "Javascript is Awesome".match(/(Javascript)?.+(Awesome)/i) +// console.log(theMatch); +// const matchedString = theMatch[0]; +// const language = theMatch[1]; +// const experience = theMatch[2]; +// console.log(matchedString, language, experience); + +// const theMatch = "is Awesome".match(/(Javascript)?.+(Awesome)/i) + +const RE_DATE = /(?[0-9]{4})-(?[0-9]{2})-(?[0-9]{2})/; + +const matchObj = RE_DATE.exec('1999-12-31'); +const year = matchObj.groups.year; // 1999 +const month = matchObj.groups.month; // 12 +const day = matchObj.groups.day; // 31 + +console.log(year, month, day); + +// ----------------- Async iterators + +const names = ['tyepescript', 'babel', 'traceur'] + +// for(let i=0; i< names.length; i++) { +// console.log(names[i]) +// } + +// for(key in names) { +// console.log(names[key]) +// } + +// names.forEach(name => { +// console.log(name); +// }) + +// for of interface is an iterable and doesn't know anything about array or object +for (name of names) { + console.log(name) +} + From 24f5ad50a8b2934808448a46dbd3635cd6b6dc3b Mon Sep 17 00:00:00 2001 From: Aman Singh Date: Tue, 30 Jul 2019 19:43:26 +0530 Subject: [PATCH 02/10] proxy and reflector challenge --- .../proxy_constructor.challenge.js | 38 +++++++++++++++++-- .../proxy_constructor.example.js | 11 +++--- Week-4/ES8/Reflect/reflect.challenge.js | 8 +++- Week-4/ES8/Reflect/reflect.example.js | 8 ++-- 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js b/Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js index 05d273e..4ab879b 100644 --- a/Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js +++ b/Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js @@ -1,4 +1,4 @@ -// 1. SET +// 1. SET // Rewrite the sample code below so that every time a property is set a callback runs. // your callback will check if the property changed is employees. @@ -11,6 +11,24 @@ let manager = { dept: `sales`, employees: 0 } +let handler = { + get: (target, propName) => { + return target[propName]; + }, + set: (target, propName, newVal) => { + if(propName==='employees'){ + if(newVal.isArray|| typeof newVal === 'string' || newVal instanceof String || newVal== null){ + target[propName] = newVal; + } else{ + console.log("its not allowed to have manager with value other than array,string or null"); + } + } else{ + target[propName] = newVal; + } + } +} +let newObj = new Proxy(manager, handler); + manager.office = `London` //updates manager.employees = ['Jim', 'Patrick', 'Mary']; //updates @@ -22,7 +40,7 @@ manager.employees = {name:'Jim'} // doesn't update // adjust the following code so that anytime an internal object with accessLevel of 1 is accessed, //"Access Denied" is returned. -const users = [ +const user = [ { username: `bob`, accessLevel: 1, @@ -39,8 +57,22 @@ const users = [ accessCode: 9999 } ] +let handlerTwo = { + get: (target, propName) => { + if(target[propName].accessLevel===1){ + return "ACCESS DENIED"; + } else{ + return target[propName]; + } + }, + + set: (target, propName, newVal) => { + target[propName] = newVal; + } +} +users = new Proxy(user, handlerTwo); console.log(users[0].username) // Access Denied console.log(users[0].accessCode) // Access Denied console.log(users[1].accessCode) // 2345 -console.log(users[2].username) // Harold \ No newline at end of file +console.log(users[2].username) // Harold diff --git a/Week-4/ES8/Proxy_Constructors/proxy_constructor.example.js b/Week-4/ES8/Proxy_Constructors/proxy_constructor.example.js index 88a3262..296023e 100644 --- a/Week-4/ES8/Proxy_Constructors/proxy_constructor.example.js +++ b/Week-4/ES8/Proxy_Constructors/proxy_constructor.example.js @@ -30,7 +30,7 @@ let handler = { // this has all the above properties to trap - // get trap // takes 2 args - target object and the property asked for + // get trap // takes 2 args - target object and the property asked for get: (target, propName) => { // console.log(target, propName); //return undefined; @@ -61,7 +61,7 @@ console.log(newObj); // } // restrict user to set non-numeric values for age -// check if the property is existed in object +// check if the property is existed in object // ---------- another example @@ -90,18 +90,19 @@ console.log(newObj); // --------------------- another example --------------- function sum (x,y){ + console.log("sum"); return x+y; } let handler2 = { //apply trap takes 3 args - target, the this, argumentlist apply: (target, thisArg, argsList) => { - console.log("someone called this function"); - // return target(argsList[0], argsList[1]) * 100 + console.log(argsList); + return target(argsList[0], argsList[1]) * 100 } } const sumProxy = new Proxy(sum, handler2); console.log(sum(2,3)); -console.log(sumProxy(2, 3)); \ No newline at end of file +console.log(sumProxy(2, 3)); diff --git a/Week-4/ES8/Reflect/reflect.challenge.js b/Week-4/ES8/Reflect/reflect.challenge.js index 7f0185f..7a3935e 100644 --- a/Week-4/ES8/Reflect/reflect.challenge.js +++ b/Week-4/ES8/Reflect/reflect.challenge.js @@ -1,7 +1,11 @@ - let course = { +}; +Reflect.defineProperty(course, 'name', {value: 'Javascript',writable: false}) +Reflect.defineProperty(course, 'duration', {value: '3 hours',writable: false}) -} +Reflect.deleteProperty(course, 'duration'); +console.log(Reflect.getOwnPropertyDescriptor(course, 'duration').value); +console.log(Reflect.getOwnPropertyDescriptor(course, 'name').value); // use reflect API to define property, delete a property, getOwnpropertydescriptor details // property to define - name with value as "Javascript", make it read only (writable : false) diff --git a/Week-4/ES8/Reflect/reflect.example.js b/Week-4/ES8/Reflect/reflect.example.js index ceb465d..ba9ca8a 100644 --- a/Week-4/ES8/Reflect/reflect.example.js +++ b/Week-4/ES8/Reflect/reflect.example.js @@ -36,8 +36,8 @@ const handler1 = { //console.log(Reflect.get(target, prop, receiver)) // arguments[0][prop] return Reflect.get(...arguments); - //fast and easy way, the win with reflect here is very explicit - //and we are accessing something inside a variable + //fast and easy way, the win with reflect here is very explicit + //and we are accessing something inside a variable } } }; @@ -66,14 +66,12 @@ function sum(...array) { return number + total; }) } - // sum.apply = function(){ // throw new Error('I broke apply function!'); // } - console.log(sum[1,2,3]); console.log(sum.apply(null, [1,2,3])); console.log(Function.apply.call(sum, null, [1,2,3])); console.log(Reflect.apply(sum, null, [1, 2, 3])); -// if proxy provides traps to change objects, Reflect provides introspection to get data about objects \ No newline at end of file +// if proxy provides traps to change objects, Reflect provides introspection to get data about objects From 41f6f3c5e6a7b06197adab9c1c4c1814ca30411d Mon Sep 17 00:00:00 2001 From: Aman Singh Date: Tue, 30 Jul 2019 19:46:53 +0530 Subject: [PATCH 03/10] proxy --- Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js b/Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js index 4ab879b..a1dbf7d 100644 --- a/Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js +++ b/Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js @@ -65,7 +65,7 @@ let handlerTwo = { return target[propName]; } }, - + set: (target, propName, newVal) => { target[propName] = newVal; } From 7bfbd5cc3800502c00d7ce9ab39ebcc36dd5fe67 Mon Sep 17 00:00:00 2001 From: Aman Singh Date: Wed, 31 Jul 2019 10:26:07 +0530 Subject: [PATCH 04/10] done challenges except tagged template --- Week-4/ES6/Symbols/symbols.challenge.js | 1 - Week-4/ES6/subclass/subclass.challenge.js | 7 +-- .../proxy_constructor.challenge.js | 45 +++++++++---------- Week-4/ES8/Reflect/reflect.challenge.js | 3 +- 4 files changed, 24 insertions(+), 32 deletions(-) diff --git a/Week-4/ES6/Symbols/symbols.challenge.js b/Week-4/ES6/Symbols/symbols.challenge.js index 98168ab..4a45ed0 100644 --- a/Week-4/ES6/Symbols/symbols.challenge.js +++ b/Week-4/ES6/Symbols/symbols.challenge.js @@ -32,7 +32,6 @@ console.log(myCar); myCar.color = `black`; myCar.make = `Jaguar`; myCar.model = `Land Rover`; - console.log(myCar); // implement the functionality to set and get values to car color - Black, car model - Land Rover and car maker - Jaguar diff --git a/Week-4/ES6/subclass/subclass.challenge.js b/Week-4/ES6/subclass/subclass.challenge.js index 5a62158..b9ef8f3 100644 --- a/Week-4/ES6/subclass/subclass.challenge.js +++ b/Week-4/ES6/subclass/subclass.challenge.js @@ -35,7 +35,6 @@ class Human extends superhero { this._healthpoints = healthpoints; this.city=city; this.state=state; - } newName() { this._name=`Mr ${this._name}`; @@ -48,8 +47,7 @@ class Human extends superhero { } } function newName() { - this._name=`Mr ${this._name}`; - + this._name=`Mr ${this._name}`; } const details = [20,"unnao","uttar pradesh","Bill", 10, 8]; const hero3 = new Human(...details); @@ -57,9 +55,6 @@ console.log(hero3); hero3.newName(); hero3.powerHalf(); console.log(hero3); - - - // hero3.name = "sai"; // console.log(hero3) // const hero4 = new superhero(20, "abc", 8); diff --git a/Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js b/Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js index a1dbf7d..3d3891a 100644 --- a/Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js +++ b/Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js @@ -12,20 +12,20 @@ let manager = { employees: 0 } let handler = { - get: (target, propName) => { - return target[propName]; - }, - set: (target, propName, newVal) => { - if(propName==='employees'){ - if(newVal.isArray|| typeof newVal === 'string' || newVal instanceof String || newVal== null){ - target[propName] = newVal; - } else{ - console.log("its not allowed to have manager with value other than array,string or null"); - } - } else{ + get: (target, propName) => { + return target[propName]; + }, + set: (target, propName, newVal) => { + if(propName==='employees'){ + if(newVal.isArray|| typeof newVal === 'string' || newVal instanceof String || newVal== null){ target[propName] = newVal; + } else{ + console.log("its not allowed to have manager with value other than array,string or null"); } - } + } else{ + target[propName] = newVal; + } + } } let newObj = new Proxy(manager, handler); @@ -58,20 +58,19 @@ const user = [ } ] let handlerTwo = { - get: (target, propName) => { - if(target[propName].accessLevel===1){ - return "ACCESS DENIED"; - } else{ - return target[propName]; - } - }, + get: (target, propName) => { + if(target[propName].accessLevel===1){ + return "ACCESS DENIED"; + } else{ + return target[propName]; + } + }, - set: (target, propName, newVal) => { - target[propName] = newVal; - } + set: (target, propName, newVal) => { + target[propName] = newVal; + } } users = new Proxy(user, handlerTwo); - console.log(users[0].username) // Access Denied console.log(users[0].accessCode) // Access Denied console.log(users[1].accessCode) // 2345 diff --git a/Week-4/ES8/Reflect/reflect.challenge.js b/Week-4/ES8/Reflect/reflect.challenge.js index 7a3935e..c49d994 100644 --- a/Week-4/ES8/Reflect/reflect.challenge.js +++ b/Week-4/ES8/Reflect/reflect.challenge.js @@ -1,5 +1,4 @@ -let course = { -}; +let course = {}; Reflect.defineProperty(course, 'name', {value: 'Javascript',writable: false}) Reflect.defineProperty(course, 'duration', {value: '3 hours',writable: false}) From c8238f253487716db2a684393abab22968304711 Mon Sep 17 00:00:00 2001 From: Aman Singh Date: Wed, 31 Jul 2019 11:57:36 +0530 Subject: [PATCH 05/10] proxy constructor done with challenge --- .../proxy_constructor.challenge.js | 49 +++++++++---------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js b/Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js index 3d3891a..a765126 100644 --- a/Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js +++ b/Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js @@ -12,24 +12,22 @@ let manager = { employees: 0 } let handler = { - get: (target, propName) => { - return target[propName]; - }, - set: (target, propName, newVal) => { - if(propName==='employees'){ - if(newVal.isArray|| typeof newVal === 'string' || newVal instanceof String || newVal== null){ - target[propName] = newVal; + get: (target, propName) => { + return target[propName]; + }, + set: (target, propName, newVal) => { + if(propName==='employees'){ + if(newVal.isArray|| typeof newVal === 'string' || newVal instanceof String || newVal== null){ + target[propName] = newVal; + } else{ + console.log("its not allowed to have manager with value other than array,string or null"); + } } else{ - console.log("its not allowed to have manager with value other than array,string or null"); + target[propName] = newVal; } - } else{ - target[propName] = newVal; - } - } + } } let newObj = new Proxy(manager, handler); - - manager.office = `London` //updates manager.employees = ['Jim', 'Patrick', 'Mary']; //updates manager.employees = 3; // doesn't update @@ -39,7 +37,7 @@ manager.employees = {name:'Jim'} // doesn't update // 2. GET // adjust the following code so that anytime an internal object with accessLevel of 1 is accessed, //"Access Denied" is returned. - +const obj={username:'Access denied' ,accessCode:'Access denied' ,accessLevel:'Access denied' }; const user = [ { username: `bob`, @@ -57,18 +55,19 @@ const user = [ accessCode: 9999 } ] + let handlerTwo = { - get: (target, propName) => { - if(target[propName].accessLevel===1){ - return "ACCESS DENIED"; - } else{ - return target[propName]; - } - }, + get: (target, propName) => { + if(target[propName].accessLevel!==1){ + return target[propName]; + } else{ + return obj; + } + }, - set: (target, propName, newVal) => { - target[propName] = newVal; - } + set: (target, propName, newVal) => { + target[propName] = newVal; + } } users = new Proxy(user, handlerTwo); console.log(users[0].username) // Access Denied From 35c0b9f7d5c97cc22f3fe461941c1c37d4288799 Mon Sep 17 00:00:00 2001 From: Aman Singh Date: Thu, 1 Aug 2019 17:57:29 +0530 Subject: [PATCH 06/10] reflect.delete property --- Week-4/ES8/Reflect/reflect.challenge.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/Week-4/ES8/Reflect/reflect.challenge.js b/Week-4/ES8/Reflect/reflect.challenge.js index c49d994..47997b4 100644 --- a/Week-4/ES8/Reflect/reflect.challenge.js +++ b/Week-4/ES8/Reflect/reflect.challenge.js @@ -1,8 +1,20 @@ -let course = {}; -Reflect.defineProperty(course, 'name', {value: 'Javascript',writable: false}) -Reflect.defineProperty(course, 'duration', {value: '3 hours',writable: false}) +let course = { + trainer:'nagasai' +}; +if(Reflect.defineProperty(course, 'name', {value: 'Javascript',writable: false})){ + console.log('property created!'); +} else{ + console.log('property not created!'); +} +if(Reflect.defineProperty(course, 'duration', {value: '3 hours',writable: false})){ +console.log('property created!'); +}else{ + console.log('property not created!'); +} +Reflect.deleteProperty(course, 'trainer') +if(Reflect.deleteProperty(course, 'duration')) +console.log('property deleted!'); -Reflect.deleteProperty(course, 'duration'); console.log(Reflect.getOwnPropertyDescriptor(course, 'duration').value); console.log(Reflect.getOwnPropertyDescriptor(course, 'name').value); From c2ee4f951371204a521d2943b139036ced1250a0 Mon Sep 17 00:00:00 2001 From: Aman Singh Date: Fri, 2 Aug 2019 15:44:34 +0530 Subject: [PATCH 07/10] pushed tagged template and async await --- .../tagged_template.challenge.js | 24 +++++++++++++++---- Week-4/ES8/Reflect/reflect.challenge.js | 14 +++++------ .../Bonus-async-await/async-awaitexample.js | 19 +++++++++++++++ 3 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 Week-4/ES9/Bonus-async-await/async-awaitexample.js diff --git a/Week-4/ES6/tagged_templates/tagged_template.challenge.js b/Week-4/ES6/tagged_templates/tagged_template.challenge.js index 075e310..bf0f728 100644 --- a/Week-4/ES6/tagged_templates/tagged_template.challenge.js +++ b/Week-4/ES6/tagged_templates/tagged_template.challenge.js @@ -12,12 +12,26 @@ let whereClauses = [ `uid = ${2+1}`, `OR city = ${city}` ] +function checkQuery(identifiers,...variables) { + let finalQuery = ""; + const queryType = variables[0].split(" ")[0]; + const table = variables[1]; + if(queryType == "UPDATE" && table == "PASSWORDS"){ + console.log("You can not change password table"); + return finalQuery; + } + if(queryType == 'UPDATE' || queryType == 'SELECT'){ + + finalQuery = (order) ? (`${variables[0]} FROM ${table} WHERE ${variables[2][0]} ${variables[2][1]} ORDER BY city`) : (`${variables[0]} FROM ${table} WHERE ${variables[2][0]} ${variables[2][1]} ORDER BY city asc`); + return finalQuery; + }else{ + console.log("only update and select query can be processed"); + return finalQuery; + } +} +var query = checkQuery `${command} FROM ${table} WHERE ${whereClauses} ${order}`; +console.log(query); // Your code to call the tag and log the return value here... // your output should be - SELECT * FROM USERS WHERE uid = 3 OR city = chicago ORDER BY asc // Spread syntax is a more common and easy here if you are comfortable with it -function checkQuery() { - //Your validation code here... - - return (finalQuery); -} diff --git a/Week-4/ES8/Reflect/reflect.challenge.js b/Week-4/ES8/Reflect/reflect.challenge.js index 47997b4..97ba8ab 100644 --- a/Week-4/ES8/Reflect/reflect.challenge.js +++ b/Week-4/ES8/Reflect/reflect.challenge.js @@ -1,20 +1,20 @@ let course = { - trainer:'nagasai' + }; +course.trainer='nagasai' if(Reflect.defineProperty(course, 'name', {value: 'Javascript',writable: false})){ console.log('property created!'); } else{ console.log('property not created!'); } if(Reflect.defineProperty(course, 'duration', {value: '3 hours',writable: false})){ -console.log('property created!'); +console.log('property duration created!'); }else{ - console.log('property not created!'); + console.log('property duration not created!'); } -Reflect.deleteProperty(course, 'trainer') -if(Reflect.deleteProperty(course, 'duration')) -console.log('property deleted!'); - +console.log(course); +Reflect.deleteProperty(course, 'trainer'); +console.log(course); console.log(Reflect.getOwnPropertyDescriptor(course, 'duration').value); console.log(Reflect.getOwnPropertyDescriptor(course, 'name').value); diff --git a/Week-4/ES9/Bonus-async-await/async-awaitexample.js b/Week-4/ES9/Bonus-async-await/async-awaitexample.js new file mode 100644 index 0000000..5dd36e8 --- /dev/null +++ b/Week-4/ES9/Bonus-async-await/async-awaitexample.js @@ -0,0 +1,19 @@ +function doubleAfter2Seconds(x) { + return new Promise(resolve => { + setTimeout(() => { + resolve(x * 2); + }, 2000); + }); +} + +async function addAsync(x) { + const a = await doubleAfter2Seconds(10); + const b = await doubleAfter2Seconds(20); + const c = await doubleAfter2Seconds(30); + return x + a + b + c; +} + + +addAsync(10).then((sum) => { + console.log(sum); +}); From 2c6e426fd28c33605a759c19e14597ecb2a09b94 Mon Sep 17 00:00:00 2001 From: Aman Singh Date: Tue, 6 Aug 2019 11:12:47 +0530 Subject: [PATCH 08/10] indentation fixed --- Week-4/ES6/Symbols/symbols.challenge.js | 42 +++++++++--------- Week-4/ES6/subclass/subclass.challenge.js | 30 ++++++------- .../tagged_template.challenge.js | 30 ++++++------- .../template_literals.challenge.js | 3 +- .../proxy_constructor.challenge.js | 44 +++++++++---------- Week-4/ES8/Reflect/reflect.challenge.js | 4 +- 6 files changed, 74 insertions(+), 79 deletions(-) diff --git a/Week-4/ES6/Symbols/symbols.challenge.js b/Week-4/ES6/Symbols/symbols.challenge.js index 4a45ed0..5b59ef4 100644 --- a/Week-4/ES6/Symbols/symbols.challenge.js +++ b/Week-4/ES6/Symbols/symbols.challenge.js @@ -3,29 +3,29 @@ const CARMAKE = Symbol(); const CARMODEL = Symbol(); class Car { - constructor(color, make, model) { - this[CARCOLOR] = color; - this[CARMAKE] = make; - this[CARMODEL] = model; - } - set color(color){ + constructor(color, make, model) { this[CARCOLOR] = color; - } - set model(model){ - this[CARMODEL] = model; - } - set make(make){ this[CARMAKE] = make; - } - get color() { - return this[CARCOLOR]; - } - get model() { - return this[CARMODEL]; - } - get make() { - return this[CARMAKE]; - } + this[CARMODEL] = model; + } + set color(color){ + this[CARCOLOR] = color; + } + set model(model){ + this[CARMODEL] = model; + } + set make(make){ + this[CARMAKE] = make; + } + get color() { + return this[CARCOLOR]; + } + get model() { + return this[CARMODEL]; + } + get make() { + return this[CARMAKE]; + } } let myCar = new Car('Red', 'Porsche', 'Cayanne'); console.log(myCar); diff --git a/Week-4/ES6/subclass/subclass.challenge.js b/Week-4/ES6/subclass/subclass.challenge.js index b9ef8f3..000ff60 100644 --- a/Week-4/ES6/subclass/subclass.challenge.js +++ b/Week-4/ES6/subclass/subclass.challenge.js @@ -30,21 +30,21 @@ class superhero { } class Human extends superhero { - constructor(healthpoints,city,state,...superherostuff) { - super(...superherostuff); - this._healthpoints = healthpoints; - this.city=city; - this.state=state; - } - newName() { - this._name=`Mr ${this._name}`; - } - powerHalf() { - this._strength /= 2; - } - powerDouble() { - this._strength *= 2; - } + constructor(healthpoints,city,state,...superherostuff) { + super(...superherostuff); + this._healthpoints = healthpoints; + this.city=city; + this.state=state; + } + newName() { + this._name=`Mr ${this._name}`; + } + powerHalf() { + this._strength /= 2; + } + powerDouble() { + this._strength *= 2; + } } function newName() { this._name=`Mr ${this._name}`; diff --git a/Week-4/ES6/tagged_templates/tagged_template.challenge.js b/Week-4/ES6/tagged_templates/tagged_template.challenge.js index bf0f728..f40c5db 100644 --- a/Week-4/ES6/tagged_templates/tagged_template.challenge.js +++ b/Week-4/ES6/tagged_templates/tagged_template.challenge.js @@ -13,22 +13,20 @@ let whereClauses = [ `OR city = ${city}` ] function checkQuery(identifiers,...variables) { - let finalQuery = ""; - const queryType = variables[0].split(" ")[0]; - const table = variables[1]; - if(queryType == "UPDATE" && table == "PASSWORDS"){ - console.log("You can not change password table"); - return finalQuery; - } - if(queryType == 'UPDATE' || queryType == 'SELECT'){ - - - finalQuery = (order) ? (`${variables[0]} FROM ${table} WHERE ${variables[2][0]} ${variables[2][1]} ORDER BY city`) : (`${variables[0]} FROM ${table} WHERE ${variables[2][0]} ${variables[2][1]} ORDER BY city asc`); - return finalQuery; - }else{ - console.log("only update and select query can be processed"); - return finalQuery; - } + let finalQuery = ""; + const queryType = variables[0].split(" ")[0]; + const table = variables[1]; + if(queryType == "UPDATE" && table == "PASSWORDS"){ + console.log("You can not change password table"); + return finalQuery; + } + if(queryType == 'UPDATE' || queryType == 'SELECT'){ + finalQuery = (order) ? (`${variables[0]} FROM ${table} WHERE ${variables[2][0]} ${variables[2][1]} ORDER BY city`) : (`${variables[0]} FROM ${table} WHERE ${variables[2][0]} ${variables[2][1]} ORDER BY city asc`); + return finalQuery; + } else{ + console.log("only update and select query can be processed"); + return finalQuery; + } } var query = checkQuery `${command} FROM ${table} WHERE ${whereClauses} ${order}`; console.log(query); diff --git a/Week-4/ES6/template literals/template_literals.challenge.js b/Week-4/ES6/template literals/template_literals.challenge.js index 3ca5251..7c4fa0b 100644 --- a/Week-4/ES6/template literals/template_literals.challenge.js +++ b/Week-4/ES6/template literals/template_literals.challenge.js @@ -6,8 +6,7 @@ const users=['aman','vijay','bharath','gagan']; function registeredMember(name){ if(users.indexOf(name) !=-1){ discount=5; - } - else{ + } else{ discount=4; } return `discount is ${discount}%` diff --git a/Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js b/Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js index a765126..ad1987e 100644 --- a/Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js +++ b/Week-4/ES8/Proxy_Constructors/proxy_constructor.challenge.js @@ -12,20 +12,20 @@ let manager = { employees: 0 } let handler = { - get: (target, propName) => { - return target[propName]; - }, - set: (target, propName, newVal) => { - if(propName==='employees'){ - if(newVal.isArray|| typeof newVal === 'string' || newVal instanceof String || newVal== null){ - target[propName] = newVal; - } else{ - console.log("its not allowed to have manager with value other than array,string or null"); - } - } else{ + get: (target, propName) => { + return target[propName]; + }, + set: (target, propName, newVal) => { + if(propName==='employees'){ + if(newVal.isArray|| typeof newVal === 'string' || newVal instanceof String || newVal== null){ target[propName] = newVal; + } else{ + console.log("its not allowed to have manager with value other than array,string or null"); } - } + } else{ + target[propName] = newVal; + } + } } let newObj = new Proxy(manager, handler); manager.office = `London` //updates @@ -57,17 +57,17 @@ const user = [ ] let handlerTwo = { - get: (target, propName) => { - if(target[propName].accessLevel!==1){ - return target[propName]; - } else{ - return obj; - } - }, + get: (target, propName) => { + if(target[propName].accessLevel!==1){ + return target[propName]; + } else{ + return obj; + } + }, - set: (target, propName, newVal) => { - target[propName] = newVal; - } + set: (target, propName, newVal) => { + target[propName] = newVal; + } } users = new Proxy(user, handlerTwo); console.log(users[0].username) // Access Denied diff --git a/Week-4/ES8/Reflect/reflect.challenge.js b/Week-4/ES8/Reflect/reflect.challenge.js index 97ba8ab..f0d12f2 100644 --- a/Week-4/ES8/Reflect/reflect.challenge.js +++ b/Week-4/ES8/Reflect/reflect.challenge.js @@ -1,6 +1,4 @@ -let course = { - -}; +let course = {}; course.trainer='nagasai' if(Reflect.defineProperty(course, 'name', {value: 'Javascript',writable: false})){ console.log('property created!'); From ea1401fe890a67020ed5e1e565eb79c528fa6b07 Mon Sep 17 00:00:00 2001 From: Aman Singh Date: Fri, 23 Aug 2019 18:15:46 +0530 Subject: [PATCH 09/10] used getters and setters --- Week-4/ES6/subclass/subclass.challenge.js | 29 +++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/Week-4/ES6/subclass/subclass.challenge.js b/Week-4/ES6/subclass/subclass.challenge.js index 000ff60..461ae7c 100644 --- a/Week-4/ES6/subclass/subclass.challenge.js +++ b/Week-4/ES6/subclass/subclass.challenge.js @@ -32,9 +32,30 @@ class superhero { class Human extends superhero { constructor(healthpoints,city,state,...superherostuff) { super(...superherostuff); - this._healthpoints = healthpoints; - this.city=city; - this.state=state; + this._healthpoints=healthpoints; + this._city=city; + this._state=state; + } + get healthpoints() { + console.log("get healthh"); + return this._healthpoints; + } + set healthpoints(healthpoints) { + this._healthpoints = healthpoints; + } + get city() { + console.log("get city"); + return this.city; + } + set city(city) { + this.city=city; + } + get state() { + console.log("get sate"); + return this._state; + } + set state(state) { + this._state = state; } newName() { this._name=`Mr ${this._name}`; @@ -54,7 +75,7 @@ const hero3 = new Human(...details); console.log(hero3); hero3.newName(); hero3.powerHalf(); -console.log(hero3); +console.log(hero3.name); // hero3.name = "sai"; // console.log(hero3) // const hero4 = new superhero(20, "abc", 8); From 57ff3b099a3d149cf78fb41b64e9ccbab1f5cd84 Mon Sep 17 00:00:00 2001 From: Aman Singh Date: Tue, 27 Aug 2019 11:06:13 +0530 Subject: [PATCH 10/10] subclass --- Week-4/ES6/subclass/subclass.challenge.js | 61 ++++++++++------------- 1 file changed, 26 insertions(+), 35 deletions(-) diff --git a/Week-4/ES6/subclass/subclass.challenge.js b/Week-4/ES6/subclass/subclass.challenge.js index 461ae7c..0d23abe 100644 --- a/Week-4/ES6/subclass/subclass.challenge.js +++ b/Week-4/ES6/subclass/subclass.challenge.js @@ -18,12 +18,18 @@ class superhero { this.strength += 5; } get name() { - console.log("get name"); return this._name; } + get strength() { + return this._strength; + } + get speed() { + return this._speed; + } set name(newname) { this._name = newname; } + static goodHero() { return true; } @@ -33,8 +39,8 @@ class Human extends superhero { constructor(healthpoints,city,state,...superherostuff) { super(...superherostuff); this._healthpoints=healthpoints; - this._city=city; - this._state=state; + this._city = city; + this._state = state; } get healthpoints() { console.log("get healthh"); @@ -43,40 +49,25 @@ class Human extends superhero { set healthpoints(healthpoints) { this._healthpoints = healthpoints; } - get city() { - console.log("get city"); - return this.city; - } - set city(city) { - this.city=city; - } - get state() { - console.log("get sate"); - return this._state; - } - set state(state) { - this._state = state; - } - newName() { - this._name=`Mr ${this._name}`; - } - powerHalf() { - this._strength /= 2; + get name() { + return `Mr ${this._name}` ; } - powerDouble() { - this._strength *= 2; + set name(newname){s + this._name=newname } + + editName = () => { + this._name = `Mr ${this._name}`; +} +powerUp = () =>{ + this._strength += 2; +} +speedDown = () => { + this._speed /= 2; +} } -function newName() { - this._name=`Mr ${this._name}`; - } const details = [20,"unnao","uttar pradesh","Bill", 10, 8]; -const hero3 = new Human(...details); -console.log(hero3); -hero3.newName(); -hero3.powerHalf(); +var hero3 = new Human(...details); console.log(hero3.name); -// hero3.name = "sai"; -// console.log(hero3) -// const hero4 = new superhero(20, "abc", 8); -// console.log(hero4); +hero3.name='abcd'; +console.log(hero3.name) \ No newline at end of file