I'm using Aptana Studio 3 to code Javascript OOP, i'm creating class and objects using the 'new' and 'this', but is really annoying because none IDE autocomplete doesn't recognize my objects properties and methods.
maybe if i change to literal notation, the IDE recognize my objects and enable the Auto Complete function, but I can't translate my code to a literal notation keeping the same functionalities.
I wrote a sample, how I usually write.
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);
If someone can convert this to literal, maybe I can figure out how it works.
Here's a small example:
var sample = function (a,b) {
this.a = a;
this.b = b;
}
var sampleObj = new sample(0,1);
sampleObj.a == 0; // true
var a = 0, b = 1;
var sample = {
a: a,
b: b
}
sample.a == 0; // true
// Would it help to call var johnsmith=ClassPerson('John','Smith'), without using the new keyword?
function ClassPerson(name, lastName){
if(!(this instanceOf ClassPerson)) return new Classperson(name, lastName);
//constructor code
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.