简体   繁体   English

JS名称空间Netbeans变量未声明

[英]JS namespace Netbeans variable not declared

When I was defining a namespace in JavaScript, Netbeans gave a global variable not declared error while I think it shouldn't. 当我在JavaScript中定义命名空间时,Netbeans给出了一个未声明错误的全局变量,而我认为它不应该。 Why is this? 为什么是这样?
Demonstration code: 演示代码:

var MyNamespace = new function () {
    var MyClass = function () {};
    MyClass.SOME_CONSTANT = 9; // MyClass not defined
    this.MyClass = MyClass; // MyClass not defined
};

I checked and this code works in IE (document mode IE 5) and Edge 13. The code is a Module Pattern like syntax like in this answer. 我检查了这个代码在IE(文档模式IE 5)和Edge 13中工作。代码是一个模块模式,就像在这个答案中的语法。

EDIT: first declaring a variable is probably not useful, for a function inside the namespace will sometimes change the local variable instead of that in the public namespace, am I right? 编辑:首先声明一个变量可能没用,因为命名空间内的函数有时会更改局部变量而不是公共命名空间中的变量,对不对? Still, I wonder why Netbeans gives me an error. 不过,我想知道为什么Netbeans会给我一个错误。

You can add a comment to the top which will tell Netbeans to ignore the error. 您可以在顶部添加注释,这将告诉Netbeans忽略该错误。 For example when using angular 例如使用角度时

/* global angular */

Will prevent the error for things like 会阻止像这样的错误

angular.module('Module', []);

I am using NetBeans IDE 8.2 (Build 201705191307) 我使用的是NetBeans IDE 8.2(Build 201705191307)

If you want to completely ignore this type of error, I would suggest this: 如果你想完全忽略这种类型的错误,我会建议:

Tools > Options > Editor > Hints > Uncheck "The global variable is not declared"

在此输入图像描述

It worked for me. 它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM