Thursday, July 9, 2015

One good way to declare Global Variables in JavaScript

In JavaScript, declaring one variable as global is very easy, some one can say that if you just write a=3; "a" will be global, and if you write var a=3; then a is local (It is not at all true..!).

Let me explain first scope in JavaScript, JavaScript is functional level scoping language (not block level as C family languages) and any variable declared inside a function is not available to outer function (parent function).

Any variable directly declared or initialized outside (not inside any function) will be global variable

        var myVar = 3; // global variable 
        function myFun(){
            var funVar = 33; // this variable is not global, scope of this variable is within "myFun" function
        }
    
Below image explain it better on console, window is the root object in a browser, so if we declare any variable outside then it comes directly under window object:

In above snapshot window.funVar is undefined because this variable is not available globally because it is inside function "myFun".

Main aim of this post is to find out best way to keep all the global variables if required in an application. One question will come in mind that, why we should avoid global variables in an application??

Normally in an application there are many JS files and if any 2 or more files have same variable name and those are global then application will be messed up and it will really hard to find the issue sometime. So we should always avoid using global variables.

Before ECMA Script 5 strict mode, there were no way to check that inside function we have declared global variable by mistake. But now using "use strict" we can find out such issues.

We can create a class to have all the global variables inside that, and using getter nd setter method we can easily get and set the variables.

// how to use Global variables in JS
    var GlobalVariables = function () {
        // keep all the variables in this GlobalVars object
        var GlobalVars = {
            var1: 123,
            var2: 456,
            var3: "values"
        }
        this.get = function (name) {
            return GlobalVars[name];
        };
        this.set = function (name, value) {
            GlobalVars[name] = value;
        };
    };

    // craeate an object to fetch all the varibales and you can set also using get and set methods
    var global = new GlobalVariables();
Usage of above global object is as shown in console below:

Wednesday, July 1, 2015

Advanced JavaScript Questions

Hello Friend,

I am sharing few questions which are most important in JavaScript. These are advanced concepts in JavaScript. You can refer my previous posts to go through them thoroughly.

  1. Hoisting concept
  2. OOPs in JavaScript
    1. Prototypal and classical model
    2. Inheritance
    3. Polymorphism
    4. Encapsulation (modeling)
  3. Closure
  4. What is prototype
  5. What is __proto__
  6. What is new in JavaScript
  7. What is bubbling
  8. Will the methods added later in prototype available in the object which is created earlier
  9. What is constructor in function prototype
  10. Scoping in JavaScript (function level vs block level)
  11. What is this
  12. How function work as constructor
  13. In how many ways we can declare functions
  14. Can you create a function which will add n numbers (n is not fixed)
  15. Number of way we can call the functions in JavaScript
  16. What is JSONP
  17. What is CORS
  18. Does inner function get access to the outer function "this"? If not how we can use "this" object inside inner function
  19. Why we use JQuery reference from CDN
  20. Is JavaScript multi-threaded or single threaded?
  21. How setTimeOut works in JavaScript? What is setInterval?
  22. What is Event Loop in JavaScript?
  23. How to create clone of a object?
  24. How to compare 2 objects are same or not?
  25. How to Swap 2 elements of an array by using Array functions available in JavaScript
  26. What is the difference between document.ready and window.load?
  27. How to check if one variable is of type Array?
  28. How can you empty an array in JavaScript? What will happen if you use arr.length = 0 ?
  29. Explain complete rendering of a page. Suppose you hit www.google.com, explain all the process step by step.
  30. How to remove one property from an Object in JavaScript
  31. What is instanceof operator in JavaScript?
  32. What are Function, Method, and Constructor in JavaScript? How to implements these in JavaScript?
  33. What is self invoking function in JavaScript and what is the use of it?
  34. What are the design pattern in JavaScript?
  35. What is singleton pattern? How we can achieve singleton pattern in JavaScript?
  36. In how many way you can create object in JavaScript?
  37. What is Publisher Subscriber architecture in JavaScript? How we can implement this?
  38. How to flatten an object in JavaScript?
  39. How to un-flatten object in JavaScript?
  40. How to prevent object modification in JavaScript? What is the difference between seal and freeze
  41. What is strict mode ("use strict") in JavaScript?
  42. Write a function to merge 2 objects dynamically in JavaScript.

Answers for all the above questions will be posted soon...