我有一个非常基本的 JS 错误,我很惭愧无法解决它...
我正在使用 ES6 和 Babel 进行开发,并且正在做一些实验。 请注意我在 Babel 中使用了这些参数:
--presets es2015 --plugins transform-es2015-modules-amd
我有一个简单的模块:
"use strict";
export default class Inspector {
static inspect() {
console.log(this.prototype.myMethod);
console.log(typeof this.prototype.myMethod);
}
}
我是这样使用这个模块的:
"use strict";
import Inspector from "inspector";
class Child extends Inspector {
myMethod() {
console.log(`Hello from ${this.name}`);
}
}
Child.inspect();
这里的目标真的很愚蠢:简单地检查原型(prototype)是如何用 ES6 继承填充的。
inspect()
方法中的第一个 console.log
如预期的那样显示:
function myMethod() { console.log("Hello from " + this.name); }
Inheritances 已经按预期工作了,hourray!
但有趣的是第二个 console.log
(console.log(typeof this.prototype.myMethod);
) 触发错误:
require.js:19 RangeError: Maximum call stack size exceeded(…)
我期待更像“功能”的东西,但是嘿,我想我很天真......
这个错误似乎与 requirejs 模块有关,但我不知道为什么我可以记录函数而不是它的类型。
另请注意,我可以在inspect
方法中调用此方法:
static inspect() {
this.prototype.myMethod();
}
这会显示“Hello from undefined”(我本以为是“Hello from Child”,但由于它不是静态方法,所以很正常。无论如何,调用已正确执行!)。
所以,我的问题是:为什么我可以记录并调用一个方法,但我不能在上面运行 typeof
?
提前致谢!
编辑:您可以在下面看到转译后的文件:
检查器.js
define(["exports"], function (exports) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
function _typeof(obj) {
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj === "undefined" ? "undefined" : _typeof(obj);
}
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
var _createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
})();
var Inspector = (function () {
function Inspector() {
_classCallCheck(this, Inspector);
}
_createClass(Inspector, null, [{
key: "inspect",
value: function inspect() {
this.prototype.myMethod();
console.log(this.prototype.myMethod);
console.log(_typeof(this.prototype.myMethod));
}
}]);
return Inspector;
})();
exports.default = Inspector;
});
child.js
function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }
define(["inspector"], function (_inspector) {
"use strict";
var _inspector2 = _interopRequireDefault(_inspector);
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
var _createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
})();
function _possibleConstructorReturn(self, call) {
if (!self) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return call && ((typeof call === "undefined" ? "undefined" : _typeof(call)) === "object" || typeof call === "function") ? call : self;
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true
}
});
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
}
var Child = (function (_Inspector) {
_inherits(Child, _Inspector);
function Child() {
_classCallCheck(this, Child);
return _possibleConstructorReturn(this, Object.getPrototypeOf(Child).apply(this, arguments));
}
_createClass(Child, [{
key: "myMethod",
value: function myMethod() {
console.log("Hello from " + this.name);
}
}]);
return Child;
})(_inspector2.default);
Child.inspect();
});
不幸的是,异常 stracktrace 不是很有用:
ea.check @ require.js:19
(anonymous function) @ require.js:23
(anonymous function) @ require.js:8
(anonymous function) @ require.js:24
x @ require.js:7
ea.emit @ require.js:24
ea.check @ require.js:20 ea.enable @ require.js:24
ea.init @ require.js:17 J @ require.js:14
h.completeLoad @ require.js:29
h.onScriptLoad @ require.js:30
EDIT2: 通过查看转译后的文件,我的 typeof
似乎被 Babel 的方法 _typeOf
替换了。而且这个函数是无限循环的...
这是 Babel 的错误吗?我是否遗漏了编译的任何参数?
最佳答案
看起来像一个 babel 错误,可能正是这个错误:https://phabricator.babeljs.io/T6777
关于javascript - Babel/RequireJS + 类型 "RangeError: Maximum call stack size exceeded",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35392666/