Suppose that I have this definition at the beginning of a file:
const ObjectId = mongoose.Types.ObjectId;
Which one should you prefer and why?
// 1
new ObjectId;
// 2
new ObjectId();
// 3
ObjectId();
The official document suggests using new ObjectId
. For me new ObjectId()
feels more natural but each of them generates a new ObjectId and I have seen examples for each of them in SO's questions and answers.
.html#types-objectid-js
Update:
Let me clear this: I know why and how to use a new operator in JavaScript, I just want to find out is there any significant difference when new
is used for ObjectID generation. (The function behaves different, throws an error, etc ...)
Suppose that I have this definition at the beginning of a file:
const ObjectId = mongoose.Types.ObjectId;
Which one should you prefer and why?
// 1
new ObjectId;
// 2
new ObjectId();
// 3
ObjectId();
The official document suggests using new ObjectId
. For me new ObjectId()
feels more natural but each of them generates a new ObjectId and I have seen examples for each of them in SO's questions and answers.
http://mongoosejs./docs/api.html#types-objectid-js
Update:
Let me clear this: I know why and how to use a new operator in JavaScript, I just want to find out is there any significant difference when new
is used for ObjectID generation. (The function behaves different, throws an error, etc ...)
-
1
MDN has a good explanation of the
new
operator (and why constructors will most likely act a bit weird if you call them without it). – Joe Clay Commented May 30, 2017 at 15:35
1 Answer
Reset to default 6There is absolutely zero difference between these instantiations. Let me go through each way and explain how it works:
new ObjectId
- This is pletely fine and does the same thing asnew ObjectId();
because you can instantiate without parentheses if the constructor does not take any arguments, see the MDN documentation on thenew
operatornew ObjectId()
- This is the "standard" way to instantiate an object and is equivalent tonew ObjectId
ObjectId()
- This is the exact same as the above two. This is because of the line in the source code:if(!(this instanceof ObjectID)) return new ObjectID(id);
The above code does the following:
a.
!(this instanceof ObjectID)
- Checks ifthis
is an instance ofObjectID
. This is only true if the constructor is called withnew
wherethis
will refer to the current instance, or elsethis
will bewindow
orundefined
depending on if you are in strict mode.b.
return new ObjectID(id)
- If the constructor is not called withnew
, then the function will returnnew ObjectID(id)
. That means if you callObjectId()
in your example, the function will detect this and returnnew ObjectID(id)
(or ifid
is not given,new ObjectID()
) so it is exactly the same as the above two options.
In conclusion, there's no functional difference, it's just up to however you like to write it. There's no reason to prefer one over the other functionally.
Note: ObjectID
and ObjectId
are the same thing. The source code states: var ObjectId = ObjectID;
.