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.
大家讲道理2017-06-26 11:00:46
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
phpcn_u15822017-06-26 11:00:46
Refer to MDN
new constructor[([arguments])]
When there is no need to pass parameters, the brackets ()
are optional
PHP中文网2017-06-26 11:00:46
var mm = function(val){
this.m = val
}
var a = new mm(1) ;
console.log(a)
var b = new mm ;
console.log(b)
过去多啦不再A梦2017-06-26 11:00:46
If the constructor does not require parameters, there is no difference between adding and not adding parentheses.