简体   繁体   English

代码完成 - Aptana Eclipse插件

[英]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. 我在过去几周一直在进行javascript开发,并尝试过JSDT和Aptana来协助代码完成。 JSDT wasn't very good at all, but I did have more luck with Aptana (used as eclipse plug-in, not the standalone product). JSDT并不是很好,但我确实有更多的运气与Aptana(用作eclipse插件,而不是独立产品)。 The problem I'm encountering is that when I create javascript classes I cannot get code completion to work. 我遇到的问题是,当我创建javascript类时,我无法使代码完成工作。 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: 我发现使用JSON样式确实有效:

var foo = {
    myMethod: function(){

    }
}

Does anyone know why Aptana works for the last style, but not the first? 有谁知道为什么Aptana适用于最后一种风格,但不是第一种? Using the JSON style won't work for me because I have to have seperate instances of the class in question. 使用JSON样式对我来说不起作用,因为我必须有相关类的单独实例。

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. 例如,如果我在javascript目录中有3个文件,那么我通常无法让Aptana在其他两个类中获取JSON样式标记。 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. 这个DID在某一点上工作(对于我创建的前两个类),但从那时起,每当我添加新类时,它们都不会被拾取。

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? 第二个是javascript中的有效类吗?

Hopefully I can help answer all of your questions related to Aptana's code completion behavior. 希望我能帮助回答有关Aptana代码完成行为的所有问题。 To encourage Aptana's code completion cooperation, I have been using this approach with success: 为了鼓励Aptana的代码完成合作,我一直在使用这种方法:

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. 但是,我发现如果我有f = new foo()但将其更改为f = new bar(),代码完成会显示普通ol'对象的属性,而不是foo或bar。 Renaming the variable (b = new bar() from f = new foo() ) or restarting the editor seems to help. 重命名变量(b =来自f = new foo()的新条形码())或重新启动编辑器似乎有所帮助。

Any ideas what the difference is? 任何想法有什么区别? Is the second a valid class in javascript? 第二个是javascript中的有效类吗?

About "new function()", according to `new function()` with lower case "f" in JavaScript , something like 关于“new function()”, 在JavaScript中使用小写“f”的`new function()` ,类似于

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. 请记住,JS中没有“类”,而是对象。 Everything is an object. 一切都是对象。

Does anyone know why Aptana works for the last [JSON] style, but not the first? 有谁知道为什么Aptana适用于最后[JSON]风格,但不是第一个?

The JSON style declaration actually creates instance of an Object named foo, so Aptana has no issue looking it up. JSON样式声明实际上创建了一个名为foo的Object实例,因此Aptana没有问题查找它。 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. 正如您所提到的,使用函数允许单独的实例,但Aptana似乎不会跟踪在找到原型之前声明为函数的事物的属性。 My reasoning is, prototype triggers Aptana's code completion because every instance of the custom object will have all the properties specified. 我的理由是,原型触发Aptana的代码完成,因为自定义对象的每个实例都将指定所有属性。 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). 如果没有原型,必须为每个实例重新定义属性(通常在构造函数中完成,但是在我最顶层的代码块中注意我的构造函数是空的,因为我使用prototype来定义自定义对象)。 This link explains more about prototype in this context http://www.phpied.com/3-ways-to-define-a-javascript-class/ 这个链接在这方面解释了有关原型的更多信息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)? Aptana中的默认JavaScript编辑器是什么(在Windows>首选项>文件关联> * .js下)? I use the Aptana JS Editor and not the JavaScript Editor (default by installation). 我使用Aptana JS编辑器而不是JavaScript编辑器(默认安装)。 Note that these settings can be different per project. 请注意,每个项目的这些设置可能不同。

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

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