Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 927
fakeguybrushthreepwood Avatar asked Sep 03 '12 05:09

fakeguybrushthreepwood


People also ask

What is the value of this in a JavaScript constructor?

In JavaScript, the thing called this is the object that "owns" the code. The value of this, when used in an object, is the object itself. In a constructor function this does not have a value.

How to create an object from a constructor function in JavaScript?

In the above example, function Person () is an object constructor function. To create an object from a constructor function, we use the new keyword. Note: It is considered a good practice to capitalize the first letter of your constructor function.

When is the constructor of a class called in JavaScript?

The constructor () method is called automatically when a class is initiated, and it has to have the exact name "constructor", in fact, if you do not have a constructor method, JavaScript will add an invisible and empty constructor method. Note: A class cannot have more than one constructor () method. This will throw a SyntaxError.

What are user defined constructors in JavaScript?

User-defined Constructors These are the constructors declared and defined by the programmer to be used throughout the application. A programmer can also define properties and methods of their own custom types. They are also known as custom constructors. By convention, all JavaScript constructors are sentence-cased.


2 Answers

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.

like image 72
smartcaveman Avatar answered Oct 04 '22 05:10

smartcaveman


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.

like image 25
Ben J Avatar answered Oct 04 '22 07:10

Ben J