The first-class citizen of JavaScript world-function __ block chain
Source: Internet
Author: User
Brief IntroductionIn many traditional languages (c/c++/java/c#, etc.), functions are present as a second-class citizen, you can only declare a function with the keyword of the language and call it, if you need to pass the function as an argument to another function, or assign to a local variable, or as a return value, We need to go through some special ways such as function pointer, Agent (delegate) and so on. And in the JavaScript world, a function is a first-class citizen, it not only has the use of all traditional functions (declarations and calls), but also can be as simple as the value of the same assignment, pass the argument, return, such a function is also called
first-level functions (first-class function)。 Not only that, functions in JavaScript also act as constructors for classes, but also as instances of function classes (instance). Such multiple identities make JavaScript functions very important.
First, JavaScript function entry levelJavaScript functions, like the general language, follow the principle of first declaring, and the function name can only contain
letters, numbers, underscores, or $, and cannot begin with a number. The following two types of declarative functions are common:-collapse Source view plain Copy to clipboard print? Direct declaration functions MyFunc function MyFunc (/* arguments/) {}//Assign anonymous function to local variable myfunc var myfunc = function (/* arguments * /) {} Note that there is a slight difference between the two types of function declarations: the first way to declare a
named function, whether it was declared in
before calling、
after the call, even the
The location that will not be executed(for example, after a return statement or a branch that will never be true), it is accessible throughout the scope; the second way is by assigning the anonymous function to a variable, which is not strictly a function declaration (function Declaration) Instead of a function expression (function expression), this function cannot be accessed by any code until the value is assigned, i.e.
assignment must be completed before the call, otherwise the call will receive an error: "typeerror:undefined is not a function". Example:-Collapse Source view plain Copy to clipboard print? Myfunc1 (); Can be called normally, because Myfunc1 adopts the way of direct declaration
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.