2011-10-19 12 views
0

我使用Aptana Studio 3編寫JavaScript OOP代碼,我使用'new'和'this'創建類和對象,但實際上很煩人,因爲沒有IDE autocomplete doesn不認識我的對象屬性和方法。將JavaScript'新對象'轉換爲文字符號

也許如果我更改爲文字表示法,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); 

如果有人可以將其轉換爲文字,也許我可以弄清楚它是如何工作的。

回答

1

這裏有一個小例子:

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 
+0

var obj = {method1:function(params){return(params);},method2:function(params){return(params);}}仍然正確嗎?私人和公共空間如何?謝謝 –

+0

你沒有得到私人範圍與對象符號,肯定有一個回退 – Joe

0

//它是否有助於調用VAR JohnSmith對= ClassPerson( '約翰','史密斯),無需使用new關鍵字?

function ClassPerson(name, lastName){ 
    if(!(this instanceOf ClassPerson)) return new Classperson(name, lastName); 
    //constructor code