JavaScript and C: Inheritance and differences
The syntax of the language is that of C but can differs to make a more dynamic code. It takes some features from the Awk and Perl languages.
There are some notable difference between the two, because JavaScript is also inspired by other languages:
- JavaScript identifiers are case-sensitive.
- Keywords must be lowercase.
- Semi-colon is optional.
- Functions may be objects.
- A function can hold other functions (they are used as methods).
- Variables have dynamic types.
- Arrays are dynamic which brings us back to AWK.
- Like in PHP, a number in a string is treated as a numeric value.
- Regular expressions are in the core of the language, that comes from Perl.
- Prototypes are used to extend objects at runtime.
- with is added to JavaScript.
- JavaScript uses a garbage-collector.
All these differences have the same goal: making the language most dynamic and facilitate the creation of scripts.
Highlights of the JavaScript syntax
Instructions and separators, the semi-colon case
The instructions are not constrained by the end of line (except a simple comment) and it uses for single separator the semicolon. But when a statement is considered complete and that the interpreter encounters a space, an end of the line, a tab, it virtually inserts a semicolon. It is better not be counting on that to not make the code unreadable.
The comma is an internal separator in instructions, for example in the header of a function it separates the arguments.
Parentheses () group lists or isolate an expression.
The squate brackets [] are used to index a table.
The syntax of comments and block of instructions is that of C
A simple comment begins with a double slashes // and continues until the end of line.
A closed comment begins with the couple of symbols /* and ends with */ without line endings have a role to play other than formatting.
It could fit on one line or holds several lines.
... code ... /* comment */
A set of instructions is isolated by these operators: "{" for the beginning of the block and "}" to the end. This set is usually associated with a command like if (condition), or a loop.
It can be used simply, it is not useful, but it is not a syntax error.
A block of instructions is considered as an instruction, as follows:
if(x == 5) document.write("ok");
is equivalent to:
if(x == 5)
{
document.write("ok");
}
With the difference that the second case can groups a set of instructions.
The with clause was added to JavaScript
This is an example of syntax that makes difference with the C language
Its role is to define a subset which uses the methods and attributes of an object without the need to mention this object.
Example:
var x = new String("--> hello");
with(x)
{
document.write(toUpperCase());
}
In this example, the method toUpperCase () of the String object is implicitly applied to the x instance thanks to with.
An example would be more useful with document in place, then we can make a simpler code using write or other methods as getElementById in series.
with(document)
{
write("--> hello");
}
The code of these demonstrations is embedded in the page and therefore displays the word "hello".
The syntax specific to instructions of the language is described in the chapters dedicated to them.
See also
Documents
- JavaScript as a first language. How to teach JavaScript.