Javascript 通过 Requirejs 传递的未定义对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11991218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:57:19  来源:igfitidea点击:

Undefined object being passed via Requirejs

javascriptrequirejs

提问by Jason Evans

I'm using Requirejsto load the JavaScript in our web app. The issues is that I'm getting an undefinedobject being passed to a module which, when used in other modules, is instantiated perfectly fine.

我正在使用Requirejs在我们的网络应用程序中加载 JavaScript。问题是我正在将一个undefined对象传递给一个模块,当在其他模块中使用时,它被很好地实例化。

OK, here's the setup. My main.jsfile which requirejs runs on startup:

好的,这里是设置。我的main.jsrequirejs 在启动时运行的文件:

require.config({
    baseUrl: "/scripts",
    paths: {
        demographics: "Demographics/demographics",
        complaints: "Complaints/complaints",
    }
});

require(["templates", "demographics", "complaints", "crossDomain"], function (templates, demographics, complaints) {
    "use strict";

    console.log("0");
    console.log(demographics === undefined);

    demographics.View.display();
});

A lot of the config has been stripped to just the core files in this problem.

许多配置已被剥离到此问题中的核心文件。

Here's Demographics.js:

这是Demographics.js

define(["ko", "templates", "complaints", "globals", "underscore"], function (ko, templates, complaints, globals) {

    // Stuff removed.
    return {
        View: view
    };
});

and Complaints.js

Complaints.js

define([
    "demographics",
    "ko",
    "templates",
    "complaints",
    "visualeffects",
    "globals",
    "webservice",
    "underscore",
    "typewatcher",
    "imagesloaded"],
    function (demographics, ko, templates, complaints, visualeffects, globals, webservice) {
        "use strict";


        console.log("1");
        console.log(demographics === undefined);
    return {
        View: view
    };
});

The problem is this - in Complaints.jsthe demographicsparameter passed via the defineconfig is undefined. The console log out tells me that "demographics === undefined" is true.

问题是 - 在通过配置传递Complaints.jsdemographics参数中defineundefined. 控制台注销告诉我“人口统计信息 === 未定义”是true.

However, when the main.js file executes, the demographics parameter passed to it is not undefined, it is, as expected, an instantiated object.

然而,当 main.js 文件执行时,传递给它的人口统计参数不是未定义的,正如预期的那样,它是一个实例化的对象。

Now I'm stuck since I can't see why in complaints.jsthat demographics variable is undefined. Can anyone spot what I'm missing please?

现在我被卡住了,因为我不明白为什么在complaints.js那个人口统计变量中是未定义的。任何人都可以发现我错过了什么吗?

回答by rharper

You have a circular dependency. The demographicsmodule depends on complaintsand complaintsdepends on demographics. As per the documentation:

你有一个循环依赖。该demographics模块依赖于complaintscomplaints依赖于demographics. 根据文档

If you define a circular dependency (a needs b and b needs a), then in this case when b's module function is called, it will get an undefined value for a.

如果你定义了一个循环依赖(a 需要 b 并且 b 需要 a),那么在这种情况下,当 b 的模块函数被调用时,它会得到 a 的未定义值。

The solution, if you can't remove the circular dependency, is to asynchronously require one of the two modules within the other on demand (say when the view is instantiated instead of when the module that defines the view is executed). Again, the docscover this topic fairly well.

如果您不能删除循环依赖,解决方案是按需异步地要求另一个模块中的两个模块之一(比如在实例化视图时而不是在执行定义视图的模块时)。同样,文档很好地涵盖了这个主题。

回答by Misha Reyzlin

Another case is when you accidentally type requireinstead of definewhen defining a module, took me some time to notice this.

另一种情况是,当您不小心键入require而不是define在定义模块时,我花了一些时间才注意到这一点。

回答by Joel

I had a similar problem. In my case, when defining a module, I'd written:

我有一个类似的问题。就我而言,在定义模块时,我写道:

define('some_dependency', ...

instead of

代替

define(['some_dependency'], ...

回答by LeeGee

Another possible reason is implementing the module's interface (AMD, CommonJS), but forgetting to return anything. I just did that.

另一个可能的原因是实现了模块的接口(AMD、CommonJS),但忘记返回任何东西。我就是这样做的。

回答by Brad Allred

I just encountered another reason:

我刚刚遇到了另一个原因:

define(function () {
    return {};
}()); // <-- notice the '()' typo.

This "typo" causes no JS errors for this one and can make it confusing to figure out especially in a complicated application with many potential circular dependancies.

这种“打字错误”不会导致此错误,尤其是在具有许多潜在循环依赖关系的复杂应用程序中,可能会导致混淆。

The reason, of course, is the "typo" is valid JS that simply calls the function you define thereby passing its result to define()rather then the function as intended.

原因当然是“错字”是有效的 JS,它只是调用您定义的函数,从而将其结果传递给define()而不是按预期传递函数。

回答by Sofia Paix?o

One other possible reason that may look obvious in hindsight is an error in your module's code. In my case, I was trying to get an attribute from an undefined variable. The error is logged in the console, but for some reason I was not seeing it/mistaking it for the undefined module error.

事后看来很明显的另一个可能原因是模块代码中的错误。就我而言,我试图从未定义的变量中获取属性。该错误记录在控制台中,但由于某种原因,我没有看到它/将其误认为是未定义的模块错误。