admin管理员组文章数量:1414946
For example, this is perfectly fine code. (The style is ES6)
import {List} from 'immutable';
console.log(List()); // List []
However, this fails.
class Foo {}
Foo(); // TypeError: Cannot call a class as a function
Further, this fails, too.
class Foo extends List {}
Foo(); // TypeError: Cannot call a class as a function
For example, this is perfectly fine code. (The style is ES6)
import {List} from 'immutable';
console.log(List()); // List []
However, this fails.
class Foo {}
Foo(); // TypeError: Cannot call a class as a function
Further, this fails, too.
class Foo extends List {}
Foo(); // TypeError: Cannot call a class as a function
Share
Improve this question
asked Sep 16, 2015 at 21:51
almostflanalmostflan
6404 silver badges15 bronze badges
11
-
1
Probably because they're not constructed using
class
? Or the ES6 transpiler was in loose mode. – Bergi Commented Sep 16, 2015 at 21:54 -
In what environment are you executing this, which
immutable
version are you using? – Bergi Commented Sep 16, 2015 at 21:55 -
immutable
version is 3.7.5 – almostflan Commented Sep 16, 2015 at 21:55 - I think your call on the transpiler is probably where it's at. Will keep investigating and report back if I find anything :) – almostflan Commented Sep 16, 2015 at 21:56
- 1 Anyway, I'm going to stop investigating. I like the workaround provided here. stackoverflow./a/31789308/1438285 – almostflan Commented Sep 16, 2015 at 22:39
1 Answer
Reset to default 9Looks like the magic for immutable
happens in their custom addon to the transpiler here.
They're basically creating their own createClass
which skips the check. This is a snippet from the transpiled (via babel
) version of my code above.
var Foo = (function (_List) {
_inherits(Foo, _List);
function Foo() {
_classCallCheck(this, Board);
_get(Object.getPrototypeOf(Foo.prototype), 'constructor', this).apply(this, arguments);
}
return Foo;
})(_immutable.List);
Where _classCallCheck
looks like this.
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function');
}
}
For immutable
, it seems the ES6 code is first transformed to already include the createClass
calls.
本文标签: javascriptWhy do immutablejs classes not require quotnewquotStack Overflow
版权声明:本文标题:javascript - Why do immutable.js classes not require "new"? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745175893a2646236.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论