使用返回javascript函数的新运算符返回奇数范围
我试图理解为什么new针对函数运行,而不是示例中函数的返回使用返回javascript函数的新运算符返回奇数范围,javascript,new-operator,Javascript,New Operator,我试图理解为什么new针对函数运行,而不是示例中函数的返回y=: function returnFunction(){ return function blah(str){ this.x = str; return this;}} y = new returnFunction()("blah") // output: Window {x: "blah"; top: Window, window: Window, location: Location, ....} x = new (return
y=
:
function returnFunction(){ return function blah(str){ this.x = str; return this;}}
y = new returnFunction()("blah")
// output: Window {x: "blah"; top: Window, window: Window, location: Location, ....}
x = new (returnFunction())("blah")
// output: blah {x: "blah"}
z = new function blah(){return this;}()
// output: blah {}
zz = new function(){return this;}() //note the missing function name
// output: Object {}
b = new function blib(str){this.x = str; return this}
// blib {x: undefined}
bb = new function blib(str){this.x = str; return this}("blah")
// blib {x: "blah"}
c = new function blib(){this.x = "blah"; return this}
// blib {x: "blah"}
因此,对于y new,创建一个returnFunction
的副本,然后调用它
y = (new returnFunction())()
通过调用匿名函数,我们没有这个
,因此它默认为窗口
在x
的情况下,通过将其包装在参数中(returnFunction
被调用,返回blah
函数),然后blah
被新的操作符设置this
调用到新对象
奇怪的是,我必须包装new(returnFunction())
以使其按正确的顺序执行
有人能给我解释一下底层执行吗?
new
的优先级高于调用参数()
。请参阅。这只是因为运算符优先级<代码>新建的优先级高于函数调用。将函数调用包装在括号中会更改求值顺序
有关详细信息,请参阅MDN上的优先级表: