代码补全——Aptana Eclipse 插件 [英] Code Completion -- Aptana Eclipse Plugin

查看:30
本文介绍了代码补全——Aptana Eclipse 插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几周我一直在做 javascript 开发,并尝试过 JSDT 和 Aptana 来帮助完成代码.JSDT 一点也不好,但我确实在 Aptana(用作 Eclipse 插件,而不是独立产品)方面运气更好.我遇到的问题是,当我创建 javascript 类时,我无法让代码完成工作.例如,如果我使用以下代码完成功能不起作用:

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(){
   }
}

我发现使用 JSON 样式确实有效:

I have found that using a JSON style does work:

var foo = {
    myMethod: function(){

    }
}

有谁知道为什么 Aptana 适用于最后一种样式,而不适用于第一种样式?使用 JSON 样式对我不起作用,因为我必须拥有相关类的单独实例.

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.

此外,我在让代码完成跨文件工作方面不是很成功.例如,如果我在 javascript 目录中有 3 个文件,那么我通常无法让 Aptana 在其他两个类中选择 JSON 样式标记.这个 DID 一度起作用(对于我创建的前 2 个类),但从那时起,每当我添加新类时,它们都不会被选中.

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.

非常感谢您的帮助.

杰瑞米

我已确定以下方法有效:

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

任何想法有什么区别?第二个是 javascript 中的有效类吗?

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

推荐答案

希望我能帮助回答您与 Aptana 的代码完成行为相关的所有问题.为了鼓励 Aptana 的代码完成合作,我一直在成功地使用这种方法:

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) }

你说

此外,我在使代码完成跨文件工作方面不是很成功.

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

但到目前为止我一直很幸运.但是,我发现如果我有 f = new foo() 但将其更改为 f = new bar(),代码完成会显示普通 ol' Object 的属性,而不是 foo 或 bar.重命名变量(b = new bar() from f = new foo() )或重新启动编辑器似乎有帮助.

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.

任何想法有什么区别?第二个是 javascript 中的有效类吗?

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

关于new function()",根据`new function()` 带有小写f";在 JavaScript 中,类似

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

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

代替

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

var foo = function(){ ... }

是实现私有属性访问的解决方法的一部分.请记住,在 JS 中没有类",而是对象.一切都是对象.

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.

有谁知道为什么 Aptana 适用于最后一种 [JSON] 样式,而不适用于第一种?

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

JSON 样式声明实际上创建了一个名为 foo 的对象的实例,因此 Aptana 查找它没有问题.正如您所提到的,使用函数允许单独的实例,但 Aptana 似乎不会跟踪在找到原型之前声明为函数的事物的属性.我的推理是,原型会触发 Aptana 的代码完成,因为自定义对象的每个实例都将具有指定的所有属性.如果没有原型,则必须为每个实例重新定义属性(通常在构造函数中完成,但请注意,在我最上面的代码块中,我的构造函数是空的,因为我使用原型来定义自定义对象).此链接在此上下文中解释了有关原型的更多信息 http://www.phpied.com/3-ways-to-define-a-javascript-class/

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/

这篇关于代码补全——Aptana Eclipse 插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆