简体   繁体   English

JavaScript:为什么我的`new`需要括号?

[英]JavaScript: Why does my `new` need parens?

I wanted to instantiate a constructor that was returned by a function, but noticed that new was a little quirky about it: 我想实例化一个函数返回的构造函数,但注意到new有点古怪:

  // This function returns a constructor function
function getConstructor(){ return function X(){this.x=true} }

getConstructor();       //=> function X(){this.x=true}
new getConstructor();  //=> function X(){this.x=true}

new (getConstructor());  //=> X {x: true}

Why are the parens needed? 为什么需要括号?

In the first case new invokes getConstructor function as a "constructor" for an object. 在第一种情况下, new调用getConstructor函数作为对象的“构造函数”。 That function returns another function (you've set it explicitly) - that's why function X(){this.x=true} is the output. 该函数返回另一个函数(您已对其进行了显式设置)–这就是为什么function X(){this.x=true}是输出的原因。

In the second case parens make the new keyword invoke the function, that was returned from getConstructor execution. 在第二种情况下,parens使new关键字调用该函数,该关键字是从getConstructor执行返回的

For better understanding: 为了更好的理解:

function getConstructor(){ return function X(){this.x=true} }

var func = getConstructor();       //=> function X(){this.x=true}
var instance = new func(); //=> X {x: true}

Because new operator has higher precedence than function call operator. 因为new运算符比function call运算符具有更高的优先级

If you want the constructor function returned by getConstructor , you have to wrap it to have the function call execute first. 如果希望getConstructor返回构造函数,则必须对其进行包装以使函数调用首先执行。

Check the Javascript Operator Precedence . 检查Javascript运算符的优先级

Without the parens, it looks like getConstructor is itself a constructor. 没有括号,看起来getConstructor本身就是一个构造函数。 Remember, new Something() attempts to create an object with the constructor Something (for example. new String() ). 请记住, new Something()尝试使用构造函数Something创建对象(例如new String() )。 But you want your constructor to be the function returned to getConstructor(), so you need parens to make the call to getConstructor() parse as a lone function call rather than as the operand of new . 但是,您希望构造函数是返回给getConstructor()的函数,因此需要parens使对getConstructor()的调用解析为一个单独的函数调用,而不是作为new的操作数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么Math Object在javascript中不需要新的关键字 - Why Math Object does not need new keyword in javascript 为什么parseJSON将我的JavaScript对象包装在一个新对象中? - Why does parseJSON wrap my JavaScript object in a new object? 为什么JavaScript需要以“;”开头? - Why does the JavaScript need to start with “;”? JavaScript新手需要将Celsius转换为Fahrenheit和Kelvin,为什么我的代码无法正常工作? - New to JavaScript need to convert Celsius to Fahrenheit and Kelvin, why won't my code work? 为什么新的Number(2)!= JavaScript中的新字符串(“2”) - Why does new Number(2) != new String(“2”) in JavaScript 为什么我的数组没有作为新行添加到我的表中? 什么时候它是未定义的? JavaScript / HTML - Why is my array not appending as a new row in my table? when it does it is undefined? JavaScript / HTML 为什么HTTP删除需要javascript才能起作用? - Why does HTTP delete need javascript to work? 为什么我的javascript不起作用 - Why does my javascript not work 为什么JavaScript for循环中的迭代器不需要分号? - Why does the iterator in a JavaScript for loop not need a semicolon? 为什么我的 functions.php 不断崩溃(wordpress)? - Javascript 的新功能 - Why does my functions.php keep crashing (wordpress)? - new to Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM