最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

anonymous function - What precisely is a construct in JavaScript? - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited May 23, 2017 at 12:25 CommunityBot 11 silver badge asked Sep 3, 2012 at 5:30 fakeguybrushthreepwoodfakeguybrushthreepwood 3,0838 gold badges42 silver badges54 bronze badges 2
  • 1 @dbaseman, read the question more carefully – smartcaveman Commented Sep 3, 2012 at 5:40
  • @u1sonderzug, Is there anything you are still confused about here? – smartcaveman Commented Sep 10, 2012 at 16:43
Add a comment  | 

3 Answers 3

Reset to default 13

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.

发布评论

评论列表(0)

  1. 暂无评论