new Thing(param) 和 new(Thing(param)) 有什么区别?
我刚才正在写一些 CoffeeScript,并收到一个奇怪的错误:
类型错误:Thing(param) 不是构造函数
但确实如此!当我在控制台中尝试时:
var that = new Thing(param);
that.doesSomething();
经过一番混乱,我查看了编译的源代码,发现 coffee
将 that = new Thing param
编译为 that = new(Thing(param));
.诡异的;我以前从未见过这样的情况。所以我立即尝试一下:tada!现在我可以复制:(
var that = new(Thing(param));
that.previousLineErrorsOut();
顺便说一下,其主页上的 CoffeeScript 生成器 生成 new Thing()
形式。情节变得更加复杂......)
我也尝试使用本机构造函数(new Worker("somefile")
和new(Worker("somefile"))
),其行为“正确”,也就是说,两种形式之间没有区别。
所以我完全困惑了:什么是 new() ?为什么在某些情况下会失败? 为什么 CoffeeScript 将我完美的 new
转换为 new()
?
I was writing some CoffeeScript just now, and getting a strange error:
TypeError: Thing(param) is not a constructor
But it is! And when I try it in the console:
var that = new Thing(param);
that.doesSomething();
After a bit of confusion, I looked through the compiled source and found out that coffee
compiles that = new Thing param
to that = new(Thing(param));
. Weird; I've never seen that before. So I promptly try it: and tada! Now I can replicate:
var that = new(Thing(param));
that.previousLineErrorsOut();
(Incidentally, the CoffeeScript generator on its home page generates the new Thing()
form. The plot thickens...)
I also try it out with native constructors (new Worker("somefile")
and new(Worker("somefile"))
), which behave "correctly", that is, there's no difference between the two forms.
So I'm thoroughly confused: what's new()
? Why is it failing in some cases? Why does CoffeeScript transform my perfectly fine new
into new()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
new
接受一个表示构造函数的表达式,以及可选的括在括号中的参数列表。例如:当您执行此操作时:
它尝试将使用参数
param
作为不带参数的构造函数调用Thing
的结果运行。new
后的括号使Thing(param)
成为表示构造函数的表达式。由于Thing
在您的情况下不返回构造函数,因此失败。它大致相当于:我不知道为什么 CoffeeScript 会这样转换它。
new
takes an expression representing a constructor and optionally a list of arguments enclosed in parentheses. For example:When you do this:
It's trying to run the result of calling
Thing
with the argumentparam
as a constructor with no arguments. The parentheses after thenew
makeThing(param)
the expression representing the constructor. AsThing
does not return a constructor in your case, it fails. It's roughly equivalent to this:I do not know why CoffeeScript transforms it that way.
类似构造函数的调用
和类似函数的调用
之间的区别在于,在第一种情况下,函数体内的
this
关键字绑定到正在创建的对象,而在第二种情况下,它绑定到全局对象(浏览器中的窗口)是
一种非常奇怪的形式,在这种形式中,首先像函数一样调用
Thing
,然后尝试其结果再次使用new
一词作为没有参数的构造函数。你的CS这么编译真是太奇怪了。
我在他的官方网站(http://jashkenas.github.com/coffee-script/,Try Coffeescript 选项卡)上尝试过,它编译
为
The difference between the constructor-like call
and the function-like call
is that in the 1st case the
this
keyword inside of the function body is bound to the object being created while in the 2nd case it's bound to the global object (a window in the browser)The
is a really strange form, and in this form the
Thing
is first called like a function, then its result is being tried agains thenew
word as a constructor without params.It is extremely strange that your CS compiles it like that.
I tried it on he official site (http://jashkenas.github.com/coffee-script/, the Try Coffeescript tab) and it compiles
to