In his book, "JavaScript: The Good Parts," Crockford emphasizes the importance of giving constructor functions names with an initial capital letter (e.g., Point) and using function names with initial capital letters only with constructor functions, while everything else should be in lowercase.
Following this convention helps prevent forgetting to use the new
operator with constructor functions.
Crockford even suggests that "[a]n even better coping strategy is to not use new
at all."
The question then arises - how can we write JavaScript code without utilizing new
altogether?
- To avoid using
new Object()
andnew Array()
, we can opt for the literal representations like{}
and[]
. - Instead of
new Number()
,new Boolean()
, andnew String()
, we can stick to0
,true
and''
. - We can forego
new RegExp()
by employing patterns such as/pattern/
.
But what about displacing new Date()
?
And perhaps most crucially, how do we manage to circumvent using new
when creating our own custom Objects?