JS namespace Netbeans variable not declared
Asked Answered
D

2

7

When I was defining a namespace in JavaScript, Netbeans gave a global variable not declared error while I think it shouldn't. 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.

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.

Downey answered 23/1, 2016 at 17:57 Comment(0)
Q
11

You can add a comment to the top which will tell Netbeans to ignore the error. For example when using angular

/* global angular */

Will prevent the error for things like

angular.module('Module', []);
Quindecennial answered 23/1, 2016 at 18:25 Comment(1)
So, is it a bug in Netbeans? I mean, I declared the variable (class) the line before the error, so it should be available in the scope, right? (and later via MyNamespace.MyClass)Downey
G
4

I am using 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"

enter image description here

It worked for me.

Gallicanism answered 15/5, 2018 at 22:50 Comment(1)
Since "parseFloat" is not a global but a native function unproperly detected as a global this might be considered a bug, and searching for a particular bug would you disable ALL of the globals warnings? Not an option at least for me, but might be practical to someone who wants to do that, but warn them :DPathological

© 2022 - 2024 — McMap. All rights reserved.