0

Aptana Studio3を使用してJavascriptOOPをコーディングしています。「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);

誰かがこれをリテラルに変換できれば、それがどのように機能するかを理解できるかもしれません。

4

2 に答える 2

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
于 2011-10-19T16:50:40.487 に答える
0

// new キーワードを使用せずに var johnsmith=ClassPerson('John','Smith') を呼び出すと役立つでしょうか?

function ClassPerson(name, lastName){
    if(!(this instanceOf ClassPerson)) return new Classperson(name, lastName);
    //constructor code
于 2011-10-19T17:45:43.523 に答える