如何避免“变量可能尚未初始化”当引用 JavaScript 父对象时?

发布于 2024-12-12 10:44:02 字数 631 浏览 1 评论 0原文

我知道有很多“如何避免这个警告”的问题,但看起来我的问题是第一个专门针对 JavaScript 的。在这种情况下,我想在它自己的声明中引用我正在初始化的东西,如下所示:(

var foo = new Foo({
  bar: new Bar({
    x: function(){
      doStuff(foo);
    }
  });
});

如果这看起来很熟悉,也许您以前使用过 ExtJS - 这就是他们的大多数 当我调用

foo.bar.x() 时,我想指向拥有 Bar (bar) 的 Foo (foo))正在调用该函数(x)。这可行,但我的 Eclipse 警告我“foo 可能尚未初始化”,引用了对 doStuff(); 的调用——因为,当引擎第一次看到该行时,我们还没有完成尚未定义 foo 。当然,除非 foo 构造成功,否则无法调用 x() ,但我的样式检查器显然还没有弄清楚这一点。

所以我不知道如何处理这个问题。我应该忽略这个警告吗?有没有办法将其标记为这样,这样我就不会再收到警告了?我这样做错了吗?我应该以不同的方式传递我的推荐信吗?

I know there are a lot of "how do I avoid this warning" questions, but it looks like mine is the first specific to JavaScript. In this case, I want to reference the thing I'm initializing inside its own declaration, like so:

var foo = new Foo({
  bar: new Bar({
    x: function(){
      doStuff(foo);
    }
  });
});

(If this looks familiar, maybe you've used ExtJS before -- this is how most of their stuff is built.)

When I call foo.bar.x(), I want to point back to the Foo (foo) that owns the Bar (bar) that's calling the function (x). This works, but my Eclipse warns me that "foo may not have been initialized", referencing the call to doStuff(); -- because, when the engine first sees the line, we haven't finished defining foo yet. Of course, x() can't be called unless foo is constructed successfully, but my style checker apparently hasn't figured that out.

So I'm at a loss for how to deal with this. Should I ignore the warning? Is there a way to mark it as such, so I don't get a warning anymore? Am I doing this wrong? Should I pass my reference in a different manner?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心碎的声音 2024-12-19 10:44:02
var Foo = function() { }

var Bar = function (obj) {
  // foo is not initialized
  obj.x();
  // called doStuff with undefined
}

var foo = new Foo({
  bar: new Bar({
    x: function(){
      doStuff(foo);
    }
  });
});

日食是对的。同样,如果您想要更好的分析系统,请考虑使用 WebStorm 3.0 或 Visual Studio 11 作为您的 JS IDE。

var Foo = function() { }

var Bar = function (obj) {
  // foo is not initialized
  obj.x();
  // called doStuff with undefined
}

var foo = new Foo({
  bar: new Bar({
    x: function(){
      doStuff(foo);
    }
  });
});

Eclipse is right. Again if you want a better analysis system consider using WebStorm 3.0 or Visual Studio 11 as your JS IDE.

终弃我 2024-12-19 10:44:02

这与 javascript 无关,但可能与 Eclipse 有关。变量 foo 可以在任何地方声明,因为变量声明是在执行任何代码之前处理的。它也可以在调用 doStuff 之前随时初始化(尽管初始化变量而不声明它们被认为是不好的形式)。

It's nothing to do with javascript, but probably something to do with Eclipse. The variable foo can be declared anywhere, since variable declarations are processed before any code is executed. It can also be initialised at any time before doStuff is called (though initialising variables without declaring them is considered bad form).

潜移默化 2024-12-19 10:44:02

首先,构造一个 Foo。向其构造函数传递一个新的 Bar,其中使用变量 foo 传递一个函数。

但是 foo 只有在 Foo 的构造函数完成后才会被赋值。此时,该函数已经使用未声明的变量 foo 进行了声明。
如果该函数在 Bar 或 Foo 的构造函数中使用,则会失败。

First, a Foo is constructed. To its constructor, a new Bar is passed, which is passed a function using a variable foo.

But foo is only assigned a value after the constructor of Foo is finished. At that point, the function is already declared, using the undeclared variable foo.
If the function is used in the constructor of either Bar or Foo, it will fail.

梦在夏天 2024-12-19 10:44:02

我认为@RobG 是对的,这是一个 Eclipse 问题,最好只是处理它。但如果你想避免它,你可以在初始化它之前声明 foo ,我敢打赌这将使 Eclipse 不会抱怨:

var foo;
foo = new Foo({
  bar: new Bar({
    x: function(){
      doStuff(foo);
    }
  });
});

I think @RobG is right, that this is an Eclipse issue and it's probably best just to deal with it. But if you wanted to avoid it, you can declare foo before you initialize it, and I bet this will keep Eclipse from complaining:

var foo;
foo = new Foo({
  bar: new Bar({
    x: function(){
      doStuff(foo);
    }
  });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文