开发者

Code Completion -- Aptana Eclipse Plugin

I have been doing javascript development for the last couple weeks and have tried JSDT and Aptana to assist in code completion. JSDT wasn't very good at all, but I did have more luck with Aptana (used as eclipse plug-in, not the standalone product). The problem I'm encountering is that when I create javascript classes I cannot get code completion to work. For example, if I use the following then code completion doesn't work:

var foo = new function(value){
   this.myMethod= function(){
   }
}

I have also verified 开发者_运维技巧that the following won't work:

function foo(value){
   this.myMethod= function(){
   }
}

I have found that using a JSON style does work:

var foo = {
    myMethod: function(){

    }
}

Does anyone know why Aptana works for the last style, but not the first? Using the JSON style won't work for me because I have to have seperate instances of the class in question.

Also, I am not very successful in getting code completion to work across files. For example, if I have 3 files in the javascript directory then I usually cannot get Aptana to pick up the JSON style markup in the other two classes. This DID work at one point (for the first 2 classes I created), but since then whenever I add new classes they aren't picked up.

Thank you very much for you assistance.

Jeremy


I have identified that the following works:

/**
* The foo function
*/
function foo() { 
}

/**
* The bar function
* @param {Object} a Object a
 * @param {Object} b Object b
 */
function bar(a, b){
};

foo.prototype.b = bar;

var x = new foo();
x.b

In the above example the key is that you are registering the method using prototype. I also tried the following, but it didn't work.

/**
* The foo function
*/
var foo = new function() { 
}

/**
* The bar function
* @param {Object} a Object a
 * @param {Object} b Object b
 */
function bar(a, b){
};

foo.prototype.b = bar;

var x = new foo();
x.b

Any ideas what the difference is? Is the second a valid class in javascript?


Hopefully I can help answer all of your questions related to Aptana's code completion behavior. To encourage Aptana's code completion cooperation, I have been using this approach with success:

var foo = function(){
}
foo.prototype.a = "a"
foo.prototype.b = function(){ alert(this.a) }

You say

Also, I am not very successful in getting code completion to work across files.

but I've had good luck so far. However, I've found that if I have f = new foo() but change it to f = new bar(), code completion shows properties of plain ol' Object as opposed to either foo or bar. Renaming the variable (b = new bar() from f = new foo() ) or restarting the editor seems to help.

Any ideas what the difference is? Is the second a valid class in javascript?

About "new function()", according to `new function()` with lower case "f" in JavaScript , something like

var foo = new function(){ ... }

instead of

var foo = { ... } // JSON style

or

var foo = function(){ ... }

is part of a workaround to implementing private access of properties. Keep in mind through all of this that there are no "classes" in JS, but rather Objects. Everything is an object.

Does anyone know why Aptana works for the last [JSON] style, but not the first?

The JSON style declaration actually creates instance of an Object named foo, so Aptana has no issue looking it up. Using a function allows separate instances, as you mention, but Aptana seems not to track properties of things that are declared as functions until prototype is found. My reasoning is, prototype triggers Aptana's code completion because every instance of the custom object will have all the properties specified. Without prototype, properties must be re-defined for each instance (generally done in the constructor function, but note in my top most code block my constructor is empty because I use prototype to define the custom object). This link explains more about prototype in this context http://www.phpied.com/3-ways-to-define-a-javascript-class/


What is your default JavaScript Editor in Aptana (under Windows > Preferences > File Associations > *.js)? I use the Aptana JS Editor and not the JavaScript Editor (default by installation). Note that these settings can be different per project.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜