Babel/RequireJS + typeof“RangeError:超过最大调用堆栈大小”

vkc1a9a2  于 2023-08-01  发布在  Babel
关注(0)|答案(2)|浏览(170)

我有一个非常基本的JS错误,我很惭愧不能解决它...
我正在用ES6和Babel开发,我正在做一些实验。请注意,我在Babel中使用了这些参数:
第一个月
我有一个简单的模块:

"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();


这里的目标真的很愚蠢:只需检查原型是如何使用ES6继承填充的。
第一个console.log from inspect()方法显示如下:
function myMethod(){ console.log(“Hello from“+this.name); }
传承果然起作用了,hourray!但有趣的是第二个console.logconsole.log(typeof this.prototype.myMethod);)会触发一个错误:
require.js:19 RangeError:已超出最大调用堆栈大小(...)
我期待的东西更像“功能”,但嘿,我想我很天真…
这个错误似乎与requirejs模块有关,但我不知道为什么我可以记录函数,但不能记录其类型。
另外请注意,我可以在inspect方法中调用此方法:

static inspect() {
        this.prototype.myMethod();
}


这将显示“Hello from undefined”(我预期是“Hello from Child”,但由于它不是静态方法,所以它是正常的。无论如何,调用正确执行!).
我的问题是为什么我可以记录和调用一个方法,但我不能在它上面运行typeof
提前感谢!

EDIT:下面是编译后的文件:
inspector.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
(匿名函数)@ require.js:23
(匿名函数)@ require.js:8
(匿名函数)@ 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

**EDIT 2:**通过查看转译的文件,似乎我的typeof被Babel的方法_typeOf替换了。这个函数无限循环。

是巴别塔的窃听器吗?我是否错过了编译的任何参数?

o7jaxewo

o7jaxewo1#

看起来像一个babel bug,它可能正是这个:https://phabricator.babeljs.io/T6777

8fq7wneg

8fq7wneg2#

对于任何人来说,Babel中的bug似乎仍然存在:https://github.com/babel/babel/issues/9127
我只是在一个用户报告我的代码有错误时遇到了它。一旦我们关闭了他正在使用的Bable转译器选项,“超出最大调用堆栈大小”错误就消失了。

**更新:**已于2020年初修复。标签:https://github.com/babel/babel/pull/11049

相关问题