我正在使用 Aptana Studio 3 编写 Javascript OOP 代码,我正在使用“new”和“this”创建类和对象,但真的很烦人,因为没有 IDE 自动完成功能无法识别我的对象属性和方法。
也许如果我更改为文字表示法,IDE 会识别我的对象并启用自动完成功能,但我无法将我的代码转换为保持相同功能的文字表示法。
我写了一个示例,我通常是如何写的。
var ClassPerson=function(name,lastName){
//initialize stuff here
alert(name + ' was just created now!');
var time=setInterval( function(){getOlder(1);} ,(1000*60*60*24*365));
//public properties
this.name=name;
this.lastName=lastName;
//private properties
var age=0;
var weight=3;
var parent=this; //reference to 'this' of this object, not the caller object.
//public methods
this.speak=function(text){
alert(text);
}
this.walk=function(steps){
weight=weight-(0.05*steps);
}
this.eat=function(kcal){
weight=weight+(kcal/2);
}
//private methods
function getOlder(years){
age=age+years;
}
}
var me = new ClassPerson('Justin','Victor');
me.speak( me.name );
me.eat(2500);
如果有人可以将其转换为文字,也许我可以弄清楚它是如何工作的。