admin管理员组

文章数量:1025457

I am developing some JavaScript to be used in a CMS I'm working on.

I have encapsulated my code like this:

(function(){
    var libraryName = {};
    ...code...
    window.libraryName = libraryName;
}())

Now when I add a subnamespace and try using it outside my declaration, the NetBeans (8.0.2) autoplete function doesn't work. Like this:

(function(){
    var libraryName = {};
    libraryName.subSet = {
            showSomething: function(){}
    };
    window.libraryName = libraryName;
}())
libraryName.subSet.showSomething(); // This works
libraryName.subSet. // No current autoplete even when pressing CTRL+space

I would like to know if there is some way to tell NetBeans how to autoplete instead of it guessing.

Thanks

I am developing some JavaScript to be used in a CMS I'm working on.

I have encapsulated my code like this:

(function(){
    var libraryName = {};
    ...code...
    window.libraryName = libraryName;
}())

Now when I add a subnamespace and try using it outside my declaration, the NetBeans (8.0.2) autoplete function doesn't work. Like this:

(function(){
    var libraryName = {};
    libraryName.subSet = {
            showSomething: function(){}
    };
    window.libraryName = libraryName;
}())
libraryName.subSet.showSomething(); // This works
libraryName.subSet. // No current autoplete even when pressing CTRL+space

I would like to know if there is some way to tell NetBeans how to autoplete instead of it guessing.

Thanks

Share Improve this question asked Feb 25, 2015 at 20:39 loco.looploco.loop 1,6171 gold badge16 silver badges27 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You can use Ctrl+K, the "hippie" code pletion. It directly pletes some matching result and if the pleted item is not what you wanted, you can keep pressing Ctrl+K to get another autopleted item (will replace the previously inserted one). Another thing, you can press Ctrl+Space 2 times to get "full" code pletion (meaning pretty much everything from other objects/variables)

Update: There is another way using JSDoc, but it works only in Dev build of NetBeans and will be part of the next 8.1 release (you can download Dev builds from here):

/**
 * @typedef libraryName
 * @property {Function} showSomething description
 * @property {someProp} foo description
 */

/**
 * @typedef someProp
 * @property {Date} day description
 * @property {Number} num description
 */
/**
 * @typedef libraryName.someProp2
 * @property {Date} day description
 * @property {Number} num description
 */

This way you'd have to create this "documentation" for your library and have it somewhere in JS file in your project (perhaps non-minified JS file of your library). With this @typedef feature, you can learn code pletion pretty much anything even if it is not even in your code). Of course there are some issues yet to be fixed (it is a Dev build)...

I tried another approach which worked for me.

I copied my JavaScript file and removed the encapsulation. So I now have two files, the "real" one with the encapsulation and another "working" one that doesn't have the encapsulation. Now when I try using the autoplete it works.

The downside for this is that you create noise since there is a file that isn't meant for the web app and you have to update it every time you update the original file. But it makes coding easier with the magic of autoplete. When you load html you just don't reference the "working" file.

So, this would be my main.js file (in /js/main.js for instance)

(function(){
var libraryName = {};
libraryName.subSet = {
        showSomething: function(){}
};
window.libraryName = libraryName;
}())

And a main.tmp.js file would be like this (in /tmp/main.tmp.js for instance)

var libraryName = {};
libraryName.subSet = {
        showSomething: function(){}
};

Now, when I do libraryName.subSet. it shows me the correct autoplete with showSomething.

I am developing some JavaScript to be used in a CMS I'm working on.

I have encapsulated my code like this:

(function(){
    var libraryName = {};
    ...code...
    window.libraryName = libraryName;
}())

Now when I add a subnamespace and try using it outside my declaration, the NetBeans (8.0.2) autoplete function doesn't work. Like this:

(function(){
    var libraryName = {};
    libraryName.subSet = {
            showSomething: function(){}
    };
    window.libraryName = libraryName;
}())
libraryName.subSet.showSomething(); // This works
libraryName.subSet. // No current autoplete even when pressing CTRL+space

I would like to know if there is some way to tell NetBeans how to autoplete instead of it guessing.

Thanks

I am developing some JavaScript to be used in a CMS I'm working on.

I have encapsulated my code like this:

(function(){
    var libraryName = {};
    ...code...
    window.libraryName = libraryName;
}())

Now when I add a subnamespace and try using it outside my declaration, the NetBeans (8.0.2) autoplete function doesn't work. Like this:

(function(){
    var libraryName = {};
    libraryName.subSet = {
            showSomething: function(){}
    };
    window.libraryName = libraryName;
}())
libraryName.subSet.showSomething(); // This works
libraryName.subSet. // No current autoplete even when pressing CTRL+space

I would like to know if there is some way to tell NetBeans how to autoplete instead of it guessing.

Thanks

Share Improve this question asked Feb 25, 2015 at 20:39 loco.looploco.loop 1,6171 gold badge16 silver badges27 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You can use Ctrl+K, the "hippie" code pletion. It directly pletes some matching result and if the pleted item is not what you wanted, you can keep pressing Ctrl+K to get another autopleted item (will replace the previously inserted one). Another thing, you can press Ctrl+Space 2 times to get "full" code pletion (meaning pretty much everything from other objects/variables)

Update: There is another way using JSDoc, but it works only in Dev build of NetBeans and will be part of the next 8.1 release (you can download Dev builds from here):

/**
 * @typedef libraryName
 * @property {Function} showSomething description
 * @property {someProp} foo description
 */

/**
 * @typedef someProp
 * @property {Date} day description
 * @property {Number} num description
 */
/**
 * @typedef libraryName.someProp2
 * @property {Date} day description
 * @property {Number} num description
 */

This way you'd have to create this "documentation" for your library and have it somewhere in JS file in your project (perhaps non-minified JS file of your library). With this @typedef feature, you can learn code pletion pretty much anything even if it is not even in your code). Of course there are some issues yet to be fixed (it is a Dev build)...

I tried another approach which worked for me.

I copied my JavaScript file and removed the encapsulation. So I now have two files, the "real" one with the encapsulation and another "working" one that doesn't have the encapsulation. Now when I try using the autoplete it works.

The downside for this is that you create noise since there is a file that isn't meant for the web app and you have to update it every time you update the original file. But it makes coding easier with the magic of autoplete. When you load html you just don't reference the "working" file.

So, this would be my main.js file (in /js/main.js for instance)

(function(){
var libraryName = {};
libraryName.subSet = {
        showSomething: function(){}
};
window.libraryName = libraryName;
}())

And a main.tmp.js file would be like this (in /tmp/main.tmp.js for instance)

var libraryName = {};
libraryName.subSet = {
        showSomething: function(){}
};

Now, when I do libraryName.subSet. it shows me the correct autoplete with showSomething.

本文标签: Is there a way to force NetBeans JavaScript AutocompleteStack Overflow