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

2024-05-14

我有一个非常基本的 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();

这里的目标真的很愚蠢:简单地检查原型是如何用 ES6 继承填充的。

首先console.log from inspect()方法显示,如预期:

函数 myMethod() { console.log("你好,来自 " + this.name); }

继承已按预期工作,小时! 但有趣的是第二个console.log (console.log(typeof this.prototype.myMethod);) 会触发错误:

require.js:19 RangeError: 超出最大调用堆栈大小(…)

我期待一些更像“功能”的东西,但是嘿,我想我很天真......

这个错误似乎与 requirejs 模块有关,但我不知道为什么我可以记录该函数但不能记录其类型。

另请注意I can在中调用这个方法inspect method:

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

这显示“Hello from undefined”(我本来期望“Hello from Child”,但由于它不是静态方法,所以这是正常的。无论如何,调用是正确执行的!)。

所以,我的问题是:为什么我可以记录并调用方法,但无法运行typeof on it?

提前致谢!

EDIT:您可以在下面看到转译的文件:

检查器.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

EDIT2:通过查看转译的文件,似乎我的typeof被替换为方法_typeOf来自巴别塔。而且这个函数无限循环......

这是 Babel 的一个错误吗?我错过了编译的任何参数吗?


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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Babel/RequireJS + typeof“RangeError:超出最大调用堆栈大小” 的相关文章

随机推荐

  • 我的设置未保存在 WordPress 主题选项页面中

    我正在尝试创建一个基于 WordPress 设置 API 的主题选项页面 当我在浏览器中检查 options php 页面时 例如http mysite com wordpress wp admin options php http mys
  • 防止 Twig 函数扩展中的输出转义

    我创建了一个 Twig 扩展 image image png 200 嗨 我知道我可以做 image image png 200 raw 但我更喜欢使用 PHP 代码 这样所有内容 来自这个 图像 扩展 都不会被转义 我看不出这可能 我知道
  • 在 anaconda 环境下运行 qsub

    我有一个程序 通常在 Linux 的 conda 环境中运行 因为我用它来管理我的库 指令如下 source activate my environment python hello world py 我怎样才能跑你好世界 py在与 PBS
  • 浏览器显示“已阻止摄像头以保护您的隐私”

    浏览器说 阻止摄像头以保护您的隐私 我的项目包括使用用户摄像头 当我从本地主机访问应用程序时 摄像头工作正常 但是当通过 IP 地址访问时 浏览器默认阻止摄像头和其他资源 我如何允许它们用于我的应用程序 我的应用程序适用于将使用 IP 地址
  • 标准 VBA 函数“找不到项目或库”

    因此 我必须在我的 PC 上运行别人的 Excel 应用程序 并且在标准函数 如日期 格式 十六进制 中间等 上收到 找不到项目或库 的信息 一些研究表明 如果我在这些函数前加上 VBA 前缀 如 VBA Date 中那样 它会正常工作 网
  • 如何检测 UISearchBar/UITextField 输入中的暂停?

    我有以下 UISearchbar 代码 void searchBar UISearchBar searchBar textDidChange NSString searchText UIApplication sharedApplicati
  • PHP strtotime() 未返回正确的月份

    由于当前月份 年份是 2012 年 1 月 为什么以下代码返回 2011 年 12 月而不是 2011 年 11 月 echo date F Y strtotime 2 months 如果有影响的话 这是在 PHP 5 3 0 上 要获得您
  • 如何使用工厂函数解决 ES6 模块中的循环依赖关系?

    我想在我的里面写这样的东西src core Chessman js file import King from chessmen King class Chessman static factory side quality switch
  • 在 FOR 循环中打印唯一值

    我有两个文件 myresult 和 annotation 两个文件中的数据似乎是范围 但事实并非如此 这就是为什么我无法将其存储在数组中 我需要使用拆分运算符 以便我可以在 for 循环中使用它并进行比较 现在我需要打印 i myresul
  • asp.net网格分页的SQL查询

    我在用iBatis and SQLServer 使用偏移量和限制进行分页查询的最佳方法是什么 也许我添加该列ROW NUMBER OVER ORDER BY Id AS RowNum 但这只会阻止简单查询的数据访问 在某些情况下 我使用选择
  • Java 中清除嵌套 Map 的好方法

    public class MyCache AbstractMap
  • Selenium 单击在 Internet Explorer 11 上不起作用

    我尝试在 Internet Explorer 上单击 selenium 但它不起作用 我努力了element click moveToElement element click build perform javascript没事了 事实上
  • ArraySlice 中的 Swift [重复]

    这个问题在这里已经有答案了 在数组上使用 prefix 方法后 我得到了所谓的 arraySlice 我怎样才能将其转换为数组 我试图从 FacebookGraphApi 获取 Ints 然后请求前 3 个 前缀 3 并尝试将它们添加到新数
  • 响应式2列2行布局

    我一直在试图弄清楚如何创建这个布局 我有一个 2 列布局 左列有 1 行 右侧有 2 行 我试图让它流畅地调整 我遇到的问题是 我希望右侧顶部图像的顶部与左侧图像的顶部对齐 而底部图像的底部与左侧图像的底部保持对齐 我应该使用桌子吗 这是我
  • python:函数中的变量,点前面是函数名

    我需要理解这个概念 其中我们可以在函数定义中的变量名中使用点 这里没有类定义 也没有模块 Python 不应该接受包含点的变量名 def f x f author sunder f language Python print x f aut
  • 在不同的 GPU 上同时训练多个 keras/tensorflow 模型

    我想在 Jupyter Notebook 中同时在多个 GPU 上训练多个模型 我正在使用 4GPU 的节点上工作 我想将一个 GPU 分配给一个模型并同时训练 4 个不同的模型 现在 我通过 例如 为一台笔记本选择 GPU import
  • 调试android数据绑定?

    谁能告诉我如何调试或找到数据绑定生成的代码 从this https www youtube com watch v NBbeQMOcnZ0链接我发现它生成了所需的代码 我猜您正在寻找自动生成的绑定 java 文件 我也在寻找他们 最后我在这
  • 从远程托管上的 PHP 获取 PHP 错误日志

    是否有 PHP 函数或其他方式以字符串形式获取 PHP 错误日志 我需要这个 因为我无法访问在其他人的服务器上运行的站点的错误日志 他提出通过电子邮件将错误日志发送给我 但这不太方便 有什么方法可以将错误日志输出到 PHP 页面吗 我意识到
  • TabLayout 的不同 tabMode

    我正在使用 ViewPager 和 TabLayout 如果选项卡可以放置在显示 tabMode 上 则它们必须是 app tabMode fixed else app tabMode scrollable 我怎样才能做到这一点 我不明白你
  • Babel/RequireJS + typeof“RangeError:超出最大调用堆栈大小”

    我有一个非常基本的 JS 错误 我很羞愧无法解决它 我正在使用 ES6 和 Babel 进行开发 并且正在做一些实验 请注意 我在 Babel 中使用了这些参数 presets es2015 plugins transform es2015