最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the difference between new ObjectId() and new ObjectId and ObjectId()? - Stack Overflow

programmeradmin5浏览0评论

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 ...)

Share Improve this question edited May 30, 2017 at 21:56 Neil Lunn 151k36 gold badges355 silver badges325 bronze badges asked May 30, 2017 at 15:14 GergoGergo 2,30023 silver badges24 bronze badges 1
  • 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
Add a ment  | 

1 Answer 1

Reset to default 6

There 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 as new ObjectId(); because you can instantiate without parentheses if the constructor does not take any arguments, see the MDN documentation on the new operator

  • new ObjectId() - This is the "standard" way to instantiate an object and is equivalent to new 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 if this is an instance of ObjectID. This is only true if the constructor is called with new where this will refer to the current instance, or else this will be window or undefined depending on if you are in strict mode.

    b. return new ObjectID(id) - If the constructor is not called with new, then the function will return new ObjectID(id). That means if you call ObjectId() in your example, the function will detect this and return new ObjectID(id) (or if id 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;.

发布评论

评论列表(0)

  1. 暂无评论