Learn Shadowing In JavaScript Day 4

  • Shadowing is an informal way for someone to learn what it is like to perform a particular job at a workplace. An individual follows around, or shadow, the worker already in that role.
  • Shadowing is not restricted to individuals who are new to the professional workforce, as it can be an equally useful tool for those in established careers who are looking to pivot to new directions.
  • There are two concepts of Shadowing:
               1) Variable Shadowing
               2) Function Shadowing
  • Outer variable or function is shadowed by the inner variable or function.
  • Inner variable or function is mask of outer variable or function.

Example :

Program-

<html>
<body>
<script>
function fun()
{
    var f1 = 10;
    alert(f1);
}
function fun()
{
    var f2 = 20;
    alert(f2);
}
fun();
</script>
</body>
</html>

Output-


IIFE :
  • An immediately involved function expression is a JavaScript programming language idiom which produces a lexical scope using JavaScript's function scoping.
  • IIFE can be used to avoid variable hoisting from within blocks, protect against polluting the global environment and simultaneously allow public access to methods while retaining privacy for variables defined within the function.
Example :

Program-

<html>
<body>
<script>
var f = 0;
(function ()
{
    f = 15;
}) ();
alert(f);
</script>
</body>
</html>

Output-


Comments

Popular posts from this blog

Basic Concept In C Sharp Day 16

Understanding C Sharp Day 18

Learn Garbage Collection Day 21