In this post, I’ll share the basic syntax that I used during the my work. For the demonstration purpose, the Chrome DevTool (v67.0) is used.
Variable Declaration
Variable. The var
statement declares a variable, optionally initializing
it to a value:
Constant. The const
declaration creates a read-only reference to a
value. The value of a constant cannot change through re-assignment, and it
cannot be redeclared:
Class
Define a new class. You can 1) define input parameters for constructor;
2) define a computed variable in constructor; 3) define an instance method such
as toEmail()
; In an instance method, when referencing an instance
variable, you must use the keyword this
— it cannot be omitted.
There can be only one method called constructor
in a class. Having more
than one occurence will throw a SyntaxError
error.
Instantiate a class instance.
Query instance members. Instance variables are accessible using the following
syntax. Method can be called in similar way, however, don’t forget the
parentheses “()
”:
instance.variable instance.method()
Comparison Operators
Equality (==
). The equality operator converts the operands if they are not
of the same type, then applies strict comparison. If both operands are objects,
then JavaScript compares internal references which are equal when operands
refer to the same object in memory.
Identity / strict equality (===
). The identity operator returns true if
the operands are strictly equal with no type conversion.
Array
Create an array.
Iterate an array. There’re many ways to achieve this:
- A simple loop
- A for…of loop
- Array.prototype.forEach()
Add an element to array.
Remove an element from array. Note that we can only remove the last one:
Function map(). Use map()
to create a new array from the
existing one:
Sorting. Sort by natural order, by numeric value, by string:
Serialization
JSON.stringify()
allows you to serialize an instance to string.
JSON.parse()
allows you to deserialize a string into an instance.