2011-10-19 9 views
0

私はAptana Studio 3を使ってJavascript OOPを作成しています。クラスとオブジェクトを 'new'と 'this'を使って作成していますが、IDE autocomplete doesn私のオブジェクトのプロパティとメソッドを認識しません。Javascript 'new object'をリテラル表記に変換する

多分、リテラル表記に変更した場合、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

//それは新しいキーワードを使用せずに、= ClassPerson( 'ジョン'、 'スミス')VaRのjohnsmithを呼び出すのに役立ちますか?

function ClassPerson(name, lastName){ 
    if(!(this instanceOf ClassPerson)) return new Classperson(name, lastName); 
    //constructor code 
関連する問題