Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. JavaScript Question Response
  2. =-=-=-=-=-=-=-=-=-=-=-=-=-=
  3. Great question!
  4.  
  5. If you are already familiar with the concept of scope then feel free to jump down to the *Hint* at the end of this explanation!
  6. ------
  7. Scope
  8. ------
  9.  
  10. This problem touches upon a very important topic known as scope.
  11. When you write a program the variables you create are accessible based upon their scope.
  12. Consider the following example:
  13.  
  14.  
  15. function A(){
  16. var message = "This is from function A";
  17. alert(message);
  18. }
  19.  
  20.  
  21. function B(){
  22. var message;
  23. messsage = "This is from function B";
  24. alert(message);
  25. }
  26.  
  27. function C(){
  28. var message = "This is from function C";
  29. function D(){
  30. alert(message);
  31. }
  32. D();
  33. }
  34.  
  35.  
  36.  
  37. Let us first consider functions A and B:
  38.  
  39. In both A and B we have a variable called message which exists within the scope of the function within which it was created.
  40.  
  41. Notice in function B I have separated the declaration and the initialization of the variable called message.
  42.  
  43. It is necessary for us to declare message in function B because the message we created in function A is no longer accesible once we leave A.
  44.  
  45. If we were to write function B without the line "var message;",
  46. then message would be considered undefined,
  47. and our assignment into that variable during the following line,
  48. messsage = "This is from function B";
  49. would fail; we would get an error message in the console log when function B gets used
  50. (which you can check by right clicking on your browser and selecting the Inspect option).
  51.  
  52. Now let's consider the curious case of function C:
  53. In function C we create another message, and we create another function called D.
  54. Inside D we don't need to declare message because D exists within the same scope as message,
  55. so calling D will alert with the value: "This is from function C".
  56.  
  57. In terms of the problem you are experiencing:
  58. the variable btnNum is still alive in the scope that your onclick functions are;
  59. this means btnNum's value is right where you left it.
  60.  
  61. The key to success for this problem will be determining how to create a new scope for each onclick function so that their btnNums live in separate scopes.
  62.  
  63.  
  64. ------
  65. *Hint*
  66. ------
  67. When you assign a function to a variable, you are assigning the actual text of that function.
  68. In order to send a function whose variables hold values it is important to initizalize the function.
  69. It is possible for a function to return another function.
  70.  
  71. Happy coding!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement