Promotion of functions and variable definitions in JavaScript

Source: Internet
Author: User

  1. <title> function Promotion </title>
  2. <script language="javascript" type="Text/javascript" >
  3. //Declare two global functions in global object, anti-pattern
  4. function foo ()
  5. {
  6. Alert ("Global foo");
  7. }
  8. function Bar ()
  9. {
  10. Alert ("Global Bar");
  11. }
  12. //define global variables
  13. var v = "Global var";
  14. function Hoistme ()
  15. {
  16. Alert (typeof foo); //function
  17. Alert (typeof bar); //undefined
  18. Alert (v); //undefined
  19. //Why is the bar function and the variable v a corresponding function variable defined in an undefined rather than a global variable?
  20. //Because functions and variables with the same name are defined inside the function, regardless of where they are defined in the function and
  21. //And variables, they will all be promoted to the top of the function.
  22. Foo (); //local Foo
  23. Bar (); //Error, missing object
  24. //function declaration, variable foo and its implementation are promoted to the top of the Hoistme function
  25. function foo ()
  26. {
  27. Alert ("local foo");
  28. }
  29. //function expression, only the variable bar is promoted to the top of the function, the implementation is not promoted
  30. var bar = function ()
  31. {
  32. Alert ("local Bar");
  33. };
  34. //define local variables
  35. var v = "local";
  36. }
  37. (Function ()
  38. {
  39. Hoistme ();
  40. })();
  41. //function expressions and variable expressions only their declarations are promoted, function declarations are declarations and implementations of functions are promoted.
  42. /** because of the effect of function promotion, the Hoistme method is equivalent to
  43. function Hoistme ()
  44. {
  45. function declaration, variable foo and its implementation are promoted to the top of the Hoistme function
  46. function foo ()
  47. {
  48. Alert ("local foo");
  49. }
  50. function expression, only the variable bar is promoted to the top of the function, the implementation is not promoted (same variable elevation)
  51. var bar = undefined;
  52. Variable declaration is promoted
  53. var v = undefined;
  54. Alert (typeof foo); function
  55. Alert (typeof bar); Undefined
  56. Foo (); Local Foo
  57. Bar (); Error, missing object
  58. bar = function ()
  59. {
  60. Alert ("local bar");
  61. };
  62. v = "local";
  63. }
  64. */
  65. </script>
  66. <body>
  67. </body>

Promotion of functions and variable definitions in JavaScript

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.