简体   繁体   中英

What precisely is a construct in JavaScript?

As I learn JavaScript I've been looking around the web and seen numerous references to constructs in Javascript, but I can't seem to find a complete definition of what they are and what they are not, especially in the context of Javascript.

For instance, in 'Similar Questions' I see links that lead to an example featuring the following code:

In What is this construct in javascript? :

(function () {

})();

From what I understand this is a construct, but what are they defined by?

Construct is a generic term referring to an arbitrary aggregate of code in a specific formation. It is not a javascript-specific term.

Basically, it can apply to anything. So, while the code you referenced is a construct known as a self invoking anonymous function, var x = "hello world"; is a construct known as a variable declaration and assignment.

A " language construct " is the full term you're looking for. According to the linked definition:

A language construct is a syntactically allowable part of a program that may be formed from one or more lexical tokens in accordance with the rules of a programming language.

So it's any valid segment of written code that follows the rules of the language. It's a generalisation of words like "expression", "statement", "function argument list", "assignment statement", "keyword", "function definition", etc. that each define a series of tokens to look for and what they will mean in the rules of the language. The code of a completed program is constructed with them.

From the Scripting Reference in MASTERING HTML4 by Deborah and Eric Ray:

Constructs are the structures that you can use in a JavaScript to control the flow of the script

They go on as to alphabetically list ALL such a controls which include Break, Comment, If, If else.... So a construct is a very specific term definition in JavaScript used to name and encompass all the statements (structures) that control the flow of a script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM