new Thing(param) 和 new(Thing(param)) 有什么区别?

发布于 2024-12-11 16:37:33 字数 852 浏览 0 评论 0原文

我刚才正在写一些 CoffeeScript,并收到一个奇怪的错误:

类型错误:Thing(param) 不是构造函数

但确实如此!当我在控制台中尝试时:

var that = new Thing(param);
that.doesSomething();

经过一番混乱,我查看了编译的源代码,发现 coffeethat = 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岛歌少女 2024-12-18 16:37:33

new 接受一个表示构造函数的表达式,以及可选的括在括号中的参数列表。例如:

new Thing;   // equivalent to next line
new Thing(); // most common form
new (function() {})(); // calls the anonymous function as a
                       // constructor with no arguments
new (function() {});   // equivalent to previous; if no arguments are
                       // specified to new, it will call with no arguments

当您执行此操作时:

new(Thing(param));

它尝试将使用参数 param 作为不带参数的构造函数调用 Thing 的结果运行。 new 后的括号使 Thing(param) 成为表示构造函数的表达式。由于 Thing 在您的情况下不返回构造函数,因此失败。它大致相当于:

var clazz = Thing(param);
var instance = new clazz();

我不知道为什么 CoffeeScript 会这样转换它。

new takes an expression representing a constructor and optionally a list of arguments enclosed in parentheses. For example:

new Thing;   // equivalent to next line
new Thing(); // most common form
new (function() {})(); // calls the anonymous function as a
                       // constructor with no arguments
new (function() {});   // equivalent to previous; if no arguments are
                       // specified to new, it will call with no arguments

When you do this:

new(Thing(param));

It's trying to run the result of calling Thing with the argument param as a constructor with no arguments. The parentheses after the new make Thing(param) the expression representing the constructor. As Thing does not return a constructor in your case, it fails. It's roughly equivalent to this:

var clazz = Thing(param);
var instance = new clazz();

I do not know why CoffeeScript transforms it that way.

且行且努力 2024-12-18 16:37:33

类似构造函数的调用

new Thing(params)

和类似函数的调用

Thing(params)

之间的区别在于,在第一种情况下,函数体内的 this 关键字绑定到正在创建的对象,而在第二种情况下,它绑定到全局对象(浏览器中的窗口)

new(Thing(params))

一种非常奇怪的形式,在这种形式中,首先像函数一样调用Thing,然后尝试其结果再次使用 new 一词作为没有参数的构造函数。

你的CS这么编译真是太奇怪了。

我在他的官方网站(http://jashkenas.github.com/coffee-script/,Try Coffeescript 选项卡)上尝试过,它编译

that = new Thing param

var that;
that = new Thing(param);

The difference between the constructor-like call

new Thing(params)

and the function-like call

Thing(params)

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

new(Thing(params))

is a really strange form, and in this form the Thing is first called like a function, then its result is being tried agains the new 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

that = new Thing param

to

var that;
that = new Thing(param);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文