Shadowing and IIFE 4

 4th Day                                                                                             See the source image

     

  • Hey Guys Welcome to my 4th day of Blog writing, I'm last year student from Shiva ji University, Kolhapur. This post has been posted for beginners and professionals both and new post will have to be updated on Every Saturday & Sunday.

          Q. Guys, What is Shadowing and how to prevent it ?

         A. Shadowing is one type of features of  JavaScript. Let's see example:

                 Input: 

            Explanation:                                                                                                                                 Above mentioned simple Example, We create a function fun()1

          and invoked fun1(); it given as output "This is fun1".

                        Output:

For Function:

        Note:

        Suppose that by mistake we created same function more than one

       times in that case 2nd function will become overlap on 1st function 

       this concept is known as Shadowing.

                          Example  Input:

            Explanation:                                                                                                                          Above two same function has been created and invoked then after 
   2nd time created function overlap on 1st so 2nd will be show and 
   1st will become hide.

                             Output: 


                                                      For Variable:
    In case of variable, more than one variable has been declared but in that 
    case both will  show output but different 1st variable will show 1 while 2nd                            will 0.
                                  Input:
                                    

                               Output:                For 1st Variable
                             
                              For 2nd Variable                    
                     
     Immediately Invoked Function Expression(IIFE)
  •      It is Prevent to the shadowing in program. Its is defined as 
  •     (function() { Statements })(); For Example:

                    Input: 

 

          Output: 
                        

                
Thank you...

Literal and Constructor way of Creating Objects 3.2

 3rd Day                                                                                           See the source image

        QHey Guys, What is the Literal and Constructor way of Creating Objects and use?

       A.1. Objects created using object literals are singletons. This means when a change is                            made to the object, it affects that object across the entire script. For Example

               Input:


                              var person ={}; // This is called literal way of creating objects. this is also comes                                      under prototypical language.

                           Output:           


                  2Constructor way of Creating Objects:

                In this case, we make a function and its attributes and also make function's object,

                   which is called Constructor way of Creating Objects. For Example

                    Input:

                       Explanation: In above Example firstly we made an object name person and its 
                        attributes like Var person = {}; is called literal way of creating objects.

                        After that , We made a ,function Human() and its attributes and also make an object                                                        (Var obj = new Human()) is called Constructor way of an objects.

                    Output:

              Prototypical: Input
                                    
                             
   
Explanation:     In above Example, We made a function name Human and mention its attributes,                                      2nd things var men = new Human(); made on Human prototype , 
             Output: 
  • As well as men.run(); will give Men will run.
  • men.behave(); will give All human should have good behavior.
       




                                                           Thank You....


Data Type and Prototype 3.1

 3rd Day                                                                                                                                See the source image

      Hey Guys welcome once again in  3rd day of my blog writing.

      Q. Hey Guys, How is the JavaScript Dynamic Language? 

     A .The same variable values change at runtime is known as Dynamic Language. So in                                   given below example values of x is changing at run time .For Example:  

                      Input:


      Note 1: Three different datatypes mentioned above example.

      1. Numeric Data Type, 2.String Data Type, 3. Boolean Data Type. 

      We have applied these three data types values in a single variable x and its result its given below .

      Note 2: Using typeof () function we can find datatype of variable.

                 For Var x=0;

  
                     x="Raushan";


                   x=true;



          Prototype: 
      JavaScript engine adds a prototype property inside a function, Prototype property 
      is basically an object (also known as Prototype object), where we can attach 
      methods and properties in a prototype object. For Example

                                                      Thank You...

                      

Undefined type and JavaScript 2.2

2nd Day                                                                                             See the source image

   Q. Hey guys ,What is hoisting in JavaScript?.

   A .  In JavaScript, first  become variable declaration and then initialization ,

         Before initialization couldn't said about the datatype. 

         If we will try to know through the JavaScript interpreter it will show undefined.

          so this concept is known as Hoisting.               

            For Example : Input


        Explanation:
In above example, 1st alert function will show as an input undefined .Because 1st alert function only hoist the declaration of the variable it will not take initialized value so due to this reason 1st alert function will give as an output undefined. And 2nd alert function will give as an output value of x.
                 
 Output:

                    1. alert fun only hoisting the variable declaration.


                     2.    

                    3.
                         

                Note: 
                Reference error and undefined are two different things.




                                                                  
Thank you...

                                                                                    
     

Global Pollution and Reference error 2.1

2nd Day                                                                                            See the source image 

   Hey Guys welcome once again in 2nd day of my blog writing.

  Q. Guys is it true?. We are declare variable in JavaScript with datatype like C, C++  etc.

  A. No, Because JavaScript variables are loosely-typed. Which means it doesn't require a datatype            to be declared. For example,

         var x;    // This is called variable declaration.                                var x=10; // This is called variable initialization      

  • Note: we can write variable like x=10;, but it may create a problem which is called global-pollution, Because its behave like global variable and anybody can modify its value. 
  • Lets see the reference error before go for global pollution.
  • Whenever we declare the variable locally and accessing globally then that moment  it is show the reference error.
  •  For Example of Reference error 
          Input:
  •       Output:
            
  •  For Example of Global pollution
  • In this Example ,we have not used var keyword due to this reason it is behave like global variable. 
  • Input:
          output:
                  

                                                              Thank you....