things to note in JS while learning them. putting from my private notes to public gist.
TODO
- hoisting
- Scope of var
- inheritence in es5
- currying
- setTimeout
to study : Named function expressions demystified
| var add = function(a){ | |
| return function(b){ | |
| if(b==undefined) return a; | |
| return add(a+b); | |
| } | |
| } | |
| //https://medium.com/@dange.laxmikant/simplified-inheritance-in-js-es5-way-60b4ff19b008 | |
| var Person = function(name, age){ | |
| this.name = name; | |
| this.age = age; | |
| } | |
| Person.prototype.getAge = function(){ | |
| return this.age; | |
| } | |
| let mya= new Person('name', 23); | |
| console.log(mya); | |
| console.log(mya.getAge()) | |
| var Emp = function(eid, name, age){ | |
| Person.call(this, name, age); | |
| this.eid = eid; | |
| } | |
| Emp.prototype = Object.create(Person.prototype); | |
| Emp.prototype.constructor = Emp; | |
| let myb = new Emp(1,'mkl',78); | |
| console.log(myb); | |
| console.log(myb.getAge()) | |
| console.log(Emp.prototype.constructor); |
| // Basically, when Javascript compiles all of your code, all variable declarations using var are hoisted/lifted to the top of their functional/local scope (if declared inside a function) or to the top of their global scope (if declared outside of a function) regardless of where the actual declaration has been made. This is what we mean by “hoisting”. | |
| // Functions declarations are also hoisted, but these go to the very top, so will sit above all of the variable declarations. | |
| // Enough talk, lets show you some basic examples of code to demonstrate the impact of hoisting.// | |
| //https://medium.com/javascript-in-plain-english/https-medium-com-javascript-in-plain-english-what-is-hoisting-in-javascript-a63c1b2267a1 | |
| console.log(myName); // undefined //no error | |
| var myName = ‘Pradeep’; | |
| ///// next file //// | |
| (function() { //IIFE | |
| theHero(); //Arrow | |
| return; | |
| function theHero() { | |
| console.log("Arrow"); | |
| } | |
| })(); | |
| //----> is changed to | |
| (function() { | |
| function theHero() { //function declaration is hoisted | |
| console.log("Arrow"); | |
| } | |
| theHero(); //Arrow | |
| return; | |
| })(); | |
| ///////////// other file ////////// | |
| //Only a function created with a function declaration is hoisted. The function in return function fn2(){}; is created with a (named) function expression so is not hoisted. | |
| var a = 0; | |
| function b(){ | |
| a= 2; | |
| function a(){ // . this function named is hoisted above (at start of this function) | |
| return 1; | |
| } | |
| console.log('inside',a) // 2 | |
| } | |
| b(); | |
| console.log(a) // 1 | |
| // with return /// | |
| var a = 0; | |
| function b(){ | |
| a= 2; | |
| return function a(){ //not hoisted | |
| return 1; | |
| } | |
| } | |
| b(); | |
| console.log(a) // 2 | |
things to note in JS while learning them. putting from my private notes to public gist.
TODO
to study : Named function expressions demystified
| //https://medium.com/@naveenkarippai/scoping-and-hoisting-in-javascript-2c2e82107427 | |
| /** | |
| In JavaScript, variables declared (using var keyword) are function-level scoped. The block-level scoping with loops and conditionals ( if,for, while, switch blocks) don’t delimit the scope. | |
| **/ | |
| var hero = "Batman"; | |
| if(true) { | |
| var hero = "The Flash"; | |
| } | |
| console.log(hero); // "The Flash" | |
| ///////////////// other file/////// | |
| var hero = "Batman"; //global scope | |
| console.log(hero); //Batman | |
| (function() { //IIFE | |
| if(true) { | |
| var hero = "The Flash"; //local scope | |
| console.log(hero); //The Flash | |
| } | |
| })(); | |
| console.log(hero); //Batman |