Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. /*
  12. If you declare a variable within a certain scope (decision block, function, or inner class).It has the same name as a variable declared in an outer scope. This is called variable shadowing. How it works. let's start with demo.
  13. */
  14. const flower="Red Rose"; // global variable.
  15. // 1. decision block
  16. if(true){
  17. const flower="Black Rose";
  18. console.log("Decision scope variable shadowing: "+flower);
  19. }
  20. console.log("Global scope variable:"+flower);
  21. // 2. function scope
  22. const foo = function(flower) {
  23. console.log("Function scope variable shadowing: "+flower);
  24. }
  25. foo("Pink Rose");
  26. // 3. Inner Class block
  27. class Garden {
  28. flower="Green Rose";
  29. constructor(flower){
  30. console.log("Default flower Name: "+flower);
  31. }
  32. getBlueRose (){
  33. const flower="Blue Rose";
  34. console.log("Inner Class scope variable shadowing :"+flower);
  35. }
  36. };
  37. const garden = new Garden("White Rose");
  38. garden.getBlueRose();
  39. </script>
  40.  
  41.  
  42.  
  43. <script id="jsbin-source-javascript" type="text/javascript">/*
  44. If you declare a variable within a certain scope (decision block, function, or inner class).It has the same name as a variable declared in an outer scope. This is called variable shadowing. How it works. let's start with demo.
  45. */
  46. const flower="Red Rose"; // global variable.
  47. // 1. decision block
  48. if(true){
  49. const flower="Black Rose";
  50. console.log("Decision scope variable shadowing: "+flower);
  51. }
  52. console.log("Global scope variable:"+flower);
  53. // 2. function scope
  54. const foo = function(flower) {
  55. console.log("Function scope variable shadowing: "+flower);
  56. }
  57. foo("Pink Rose");
  58. // 3. Inner Class block
  59. class Garden {
  60. flower="Green Rose";
  61. constructor(flower){
  62. console.log("Default flower Name: "+flower);
  63. }
  64. getBlueRose (){
  65. const flower="Blue Rose";
  66. console.log("Inner Class scope variable shadowing :"+flower);
  67. }
  68. };
  69. const garden = new Garden("White Rose");
  70. garden.getBlueRose();
  71.  
  72. </script></body>
  73. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement