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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With