alm*_*lan 7 javascript ecmascript-6 immutable.js
例如,这是完美的代码.(风格是ES6)
import {List} from 'immutable';
console.log(List()); // List []
Run Code Online (Sandbox Code Playgroud)
但是,这失败了.
class Foo {}
Foo(); // TypeError: Cannot call a class as a function
Run Code Online (Sandbox Code Playgroud)
此外,这也失败了.
class Foo extends List {}
Foo(); // TypeError: Cannot call a class as a function
Run Code Online (Sandbox Code Playgroud)
看起来神奇的immutable
事情发生在这里的转换器的自定义插件中.
他们基本上是创造自己的createClass
,跳过支票.这是babel
我上面代码的转换(via )版本的片段.
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);
Run Code Online (Sandbox Code Playgroud)
哪里_classCallCheck
是这样的.
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function');
}
}
Run Code Online (Sandbox Code Playgroud)
因为immutable
,似乎ES6代码首先转换为已包含createClass
调用.