Excuse me, what is the difference between adding or not adding parentheses to the mm function after new? Why do I get the same results? Please ask a senior expert to explain the principle inside.
First of all, for this kind of basic question, you need to learn to consult the official documentation. The documentation has detailed instructions: new foo is equivalent to new foo(), which can only be used without passing any parameters. Of course, you can’t completely trust the documentation. , after all, the pitfalls of js. . Then what’s the difference? There are detailed descriptions in this priority summary: new (with parameter list) has a priority of 19, and new (without parameter separation) has a priority of 18, so new foo() will be executed first
Finally, I remember seeing an article not long ago about a disgusting interview question, which was: new f(), new f, new f.g(), new f().g(), etc. Regarding the priority calculation problem, you can search it yourself
First of all, for this kind of basic question, you need to learn to consult the official documentation.
The documentation has detailed instructions: new foo is equivalent to new foo(), which can only be used without passing any parameters.
Of course, you can’t completely trust the documentation. , after all, the pitfalls of js. .
Then what’s the difference? There are detailed descriptions in this
priority summary: new (with parameter list) has a priority of 19, and new (without parameter separation) has a priority of 18, so new foo() will be executed first
Finally, I remember seeing an article not long ago about a disgusting interview question, which was: new f(), new f, new f.g(), new f().g(), etc. Regarding the priority calculation problem, you can search it yourself
Refer to MDN
When there is no need to pass parameters, the brackets
()
are optionalvar mm = function(val){
}
var a = new mm(1) ;
console.log(a)
var b = new mm ;
console.log(b)
If the constructor does not require parameters, there is no difference between adding and not adding parentheses.