Dec 14, 2016

Hoisting in Javascript

You might have heard of Flag hoisting , but what the fuck is hoisting in JavaScript ?
There are two types of hoisting, variable hoisting and function hoisting.


Variable hoisting :


By default JavaScript move all declarations to the beginning of the current scope or current function.

Example 1 : Variable hoisting in very outer scope

x = 5; // Assign/defining 5 to x
console.log(x)                  
var x; // Declare x 
BUT this is How it will be Executed
var x; // Declare x 
x = 5; // Assign/defining 5 to x
console.log(x)   
Observation : See the difference var x; // Declare x   is set to the beginning for scope  

Tips to write immortal test cases

1 : Do not test internal implementation just check out side functionality

Eg : 
Function add(a , b){
Var c = a + b + 'hello';
}
So you  should not test what value is assigned to c or a or b inside add function.
You only have to see what input you give and what output you get from add function


2 : Write testable code that includes callbacks if Possible

3 : Review your test case from other person who does not know what are you testing and should be able to figure out what test you have written.
Because there will be time when there will be 1000 thousands of test cases and even you won't be able to figure out which one does what.

4 : Create a habit of writing test case before writing code as this will be time when you will be including callbacks if that code has completed its task...refer to point no 2

5 : Automate your test cases and run them daily, let them fail you don't need to worry , those failures indicate incomplete or buggy code that's scattered all over your app

6 : Discover couple of debugging environments for testing your test case not just one way...

7 : find the easiest way to run test cases. It should not be couple of steps to run test case. It should be one liner, small configuration, one simple button.

8 : it's like if test cases are hard to run then no body other than you will run test cases

9: even if there are couple of steps to run test cases then try to configure those couple of steps in one liner .

10 : Learn to differentiate between unit and integration test. Many people write Integration tests thinking they are writing unit test case.People faiI to recognize the boundary of unit test and pull that unit test toward integration test .

11 :  how Integration testing will help

Nov 29, 2016

Closures In Javascript

Actual understanding : Closure is a function that stays alive or connected to its parent Scope or parent function even after Parent scope or parent functions execution is over.


Problem : Suppose you are told to write a function that increments counter value by 1, What will you do ?


Obviously We would Code
var counter = 0;

function add() {
    return counter += 1;
}
add(); //Outputs 1
add(); //Outputs 2
add(); //Outputs 3

Jun 13, 2016

Javascript Prototypes For Performance

Without Prototype : Lots of memory per object
  • Each time when you create object from below vehicle function,
  • the line function(name){  this.name = name;  }  is alocated memory each time the object is created,
  • so there are multiple instances of same function.
function vehicle(){
    this.name = 'truck';
    this.set_name = function(name){ 
        this.name = name; 
    }
}
var taxi = new vehicle();

Mar 23, 2016

Ways to export modules in nodejs

there are several ways to export modules in nodejs

  1. Simplest export
  2. Define a global
  3. Export by anonymous function
  4. Export by named function
  5. Export by anonymous object
  6. Export by named object
  7. Export by anonymous prototype
  8. Export by named prototype
  9. Export by Json

1. Simplest export

//hello.js
console.log('Hello World');

// app.js
require('hello.js');