In the video lessons, the author encountered the expression syntactic sugar , what does it mean?
- oneI once wrote a question for a quiz about this term. I tried to briefly and clearly formulate: “This addition of the syntax of a programming language does not add new features, but only facilitates the use of language by man.” (19 letters) - Sasha Chernykh
2 answers
The phrase " syntactic sugar " is used to describe syntactic structures that are introduced only to simplify the implementation of something (reduce the amount of code, improve readability, etc.) in a particular language.
At the same time, it is quite possible to do without syntactic sugar, but the implementation without using it will be more cumbersome (complex, incomprehensible, ...).
A typical example is the new syntax for switch functions in ES6:
var f = x => x*x; In ES5, this construction can be written as:
var f = function(x) { return x*x; } For fans of JS, I note that the switch function is also the context of the call "bind" automatically, but in this particular example it is not essential.
See Wikipedia for details.
- "arrow function" is the new name of the lambda function? ) - Nick Volynkin ♦
- The " arrow function " is a subtype of anonymous (lambda) functions in JS. In the English version, the well-established term "arrow function". And it is called so according to the characteristic arrow
=>in the syntax of the definition - Dmitriy Simushev - why subspecies than differ from others? - Nick Volynkin ♦
- oneA slightly different syntax + automatic binding
this. As far as I remember, theFunction.prototype.bindmethod is also not applicable to them - Dmitriy Simushev - Yeah, I got it. Thank you) - Nick Volynkin ♦
Syntactic sugar is a language design that completely duplicates existing possibilities, but at the same time has the advantage of convenience / brevity / similarity / style.
In this case, the "ternary if statement" completely coincides with the typical if-else and assignment, but a bit shorter.
a = x != 0 ? a/x : 0; similar but shorter than
if(x != 0){ a /= x; } else { a = 0; }