About function A () {} and var a=function () {}

Source: Internet
Author: User

Cases:

a (); B (); function A () {Console.log (111);} var b = function () {Console.log (222);}

Run Discovery 111, B is not a function;

This is because function a () {} is pre-compiled before the code executes.

What is the pre-compilation of JS?

JS will explain both the Var and function declaration keywords in advance, and the assignment of variables is done when the code executes.

That is: var B = function () {} at the pre-compilation stage, the equivalent of Var b, and at execution time, because from top to bottom execution, so when executed to B (), the browser only know that Var B, and has not been assigned (undefined), so reported B is not a functio N error.

About function A () {} and var a=function () {}

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.