var variable reference function and direct declaration function in JavaScript

Source: Internet
Author: User

Today in the process of developing the app in H5 encountered a JS problem, function of the implementation of the problem

There are two ways of declaring function functions in JS

var a=function () {...}

Or

function A () {...}

The first is called a variable reference function, because JS allows a function to be passed as a value to a variable, and the second is a direct declarative function

I always thought that these two kinds of differences, however, today in the use of the process found that when I use the first way to declare function, if the following:

document.getElementById ("xx"). AddEventListener ("tap", A), Var a=function () {...} Clicking Tap does not perform a

The reason is simple, if you use the first method, that is, the function is assigned as a value to the variable A, you must first declare a, and assign a value, a basic programming Knowledge: No declared variables can not be used (in memory does not exist), the variable declaration must be used before the In an instance, AddEventListener never get a!

However, if the second method, that is, the method of directly declaring the function, then the browser after the end of the DOM, the function is already there, can call

The var variable = method defines a function that cannot be called before the function is declared, only the function is declared and then called.

function methods define functions that can be called first and then declared.


var variable reference function and direct declaration function 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.