我正在使用 Aptana Studio 3 编写 JavaScript;它对内置类型具有出色的内容帮助,它可以正确地从变量的源中推断出变量的类型,并且在编写以字符串文字编写代码的代码以及在 HTML 文档中以字符串文字编写代码时,它是一个救生员。也就是说,内容辅助和支持 ScriptDoc 功能一直令人困惑、缓慢且令人恼火。当我尝试编写类/构造函数并记录函数时,我可以使用 ScriptDoc 标签来获得内容帮助:
- 无法识别函数/类的名称;
- 将构造函数识别为函数,将类识别为类型,但无法将该识别传播到变量。只要我想写我的课并且从不使用它,这很好;
- 将类识别为类型,但无法将构造函数理解为函数。也就是说,它不会详细说明函数的参数、描述或返回类型,但如果我实例化它,它将帮助我了解对象的成员。
可以在http://wiki.appcelerator.org/display/tis/ScriptDoc+%28SDOC%29+2.0+Specification找到 ScriptDoc 功能的文档(Aptana 从中获取用户定义的内容辅助信息);但是,Aptana 无法识别列出的许多关键字,并识别出一些不存在的关键字。例如,“@classDescription”在文档中列出但未被识别,而“@class”被识别但未在文档中列出。另请注意,完全按照他们的描述做事根本不起作用。
任何人都可以帮助我提供一个记录的 JavaScript“类”示例,以便 Aptana Studio 3 代码辅助将正确描述类和构造函数的参数,正确推断分配变量的类型,并正确推断一个返回的变量的类型它的方法,就像它与本机类型一样?使用下面的代码,根据需要添加注释块和标签。我已经删除了我的大部分内容,因为我一直在弄乱它们,因为它们不起作用。
/**
* Constructor for Vector class
*/
function Vector(nX, nY) {
this.x = nX || 0;
this.y = nY || 0;
}
Vector.prototype = {
x: 0,
y: 0,
/**
* make a new Vector out of me
*/
copy: function () {
return new Vector(this.x, this.y);
},
/**
* compare to some other vector. Are they equal?
* @param {Vector} vOther some other Vector
*/
equals: function (vOther) {
//vOther should have content assistance, too.
return (vOther.x === this.x) && (vOther.y === this.y);
}
};
var v = new Vector(1,2); //Should describe Vector class/constructor, types & purposes of nX & nY (Numbers)
var c = v.copy(); //Should recognize v as a Vector and describe v.copy()
c.copy(); //If c.copy() is described properly, return type is correctly deduced & you win!
//bonus points if you can get it to inherit from something and describe c.inheritedMethod(someParameter)
谢谢!
更新:在没有对 Aptana 的 Jira、Tenderapp 或 StackOverflow 的决定性回应的情况下,我开发了一个可怕的 hack,任何人都不应该使用它。无论如何,我将它包含在这里有两个原因:它可能会为开发人员确定问题的根本原因提供信息,并且它可能会激励他们解决问题以防止人们使用黑客。它是这样的:
// Only recognizes global names
/**
* This constructor will still be listed as returning 'none', but successfully infers
* the type of a 'new' expression. Adding a return tag will break this effect.
* @constructor (can't tell if this tag does anything)
*/
MyClass = function () {
// properties added here still won't work
}
/**
* Describes an obvious property.
* @type {String}
*/
MyClass.prototype.obviousProperty = "obvious";
// only works for properties declared like that
/**
* Logs a comment on the parameter's property and returns this object (for chaining)
* @param {MyClass} oProperty This is what you see for help on calling this method,
* but it doesn't affect CA inside the method
* @return {MyClass} This makes the CA for calling the method correctly list
* the return type, but doesn't cause inference of the
* returned value's type.
*/
MyClass.prototype.commentOn = function (oProperty) {
// hack below makes CA work when you type oProperty.
log("obvious property is " + oProperty.obviousProperty);
// the type of 'this' is not understood; I don't even know if the ScriptDoc
// standard has a mechanism for it
return this;
// BEGIN HACK (note that this code is unreachable)
// force working inference from assignment directly to symbol
oProperty = new MyClass;
// force working inference of return type
return new MyClass;
// END HACK
}
var foo = new MyClass; // see class description from above
var bar = new MyClass; // see it again, so it's not a crazy fluke
var baz = foo.commentOn(bar); // CA suggests & documents commentOn
baz. // CA suggests & documents obviousProperty & commentOn
它之所以有效,是因为代码成功地导致了推理,并且 ScriptDoc 成功地将文档附加到代码中,但是 ScriptDoc 无法自行引发推理(或者以一种似乎没有人能够弄清楚的非常糟糕的方式进行推理)。我的赌注仍然是 ScriptDoc 对类型名称的选择,这在索引视图中很明显。在 Aptana Studio 3.08 上,它将我的所有类型都列为“动态类型”名称,而不是人们可以合理理解它们的名称。在 2012 年 2 月 2 日的每晚构建中,它现在将我的所有构造函数分别列为Function<NameOfClass>
和列表NameOfClass.prototype
(似乎对 ScriptDoc 或推理没有帮助)。
我已经在记事本中编写了代码,所以这不应该像我觉得的那么重要,但我仍然非常感谢一个非黑客的答案。谢谢!
更多更新:
对 Aptana 源代码的详尽调查揭示了文档和列出的功能与实际实现之间的许多差异。你可以在https://jira.appcelerator.org/browse/APSTUD-4454看到我的笔记。