1

JavascriptMVCを使用した最初のプロジェクトに取り組んでいます。

クラスFooがあります。

$.Class('Foo',{
    // Static properties and methods
    message: 'Hello World',
    getMessage: function() {
        return Foo.message;
    }
},{});

これはうまくいきます。しかし、クラス名がわからない場合はどうすればよいでしょうか。私はこのようなものが欲しい:

$.Class('Foo',{
    // Static properties and methods
    message: 'Hello World',
    getMessage: function() {
        return this.message;
    }
},{});

しかし、静的プロパティではこれを使用できません。では、静的メソッドから現在のクラス名を取得するにはどうすればよいですか。

プロトタイプ メソッドからは簡単です。

this.constructor.shortName/fullName.

しかし、静的メソッドでそれを行う方法は?

4

1 に答える 1

0

真実は、私が間違っていたということです。これを静的メソッドで使用することは可能です。以下は、JavascriptMVC の静的およびプロトタイプ メソッドとプロパティがどのように機能するかを理解するのに役立つ小さなコード スニペットです

$.Class('Foo', 
{
  aStaticValue: 'a static value',
  aStaticFunction: function() {
    return this.aStaticValue;
  }
}, 
{
  aPrototypeValue: 'a prototype value',
  aPrototypeFunction: function() {
    alert(this.aPrototypeValue); // alerts 'a prototype value'
    alert(this.aStaticValue); // alerts 'undefined'
    alert(this.constructor.aStaticValue); // alerts 'a static value'
  }
});

alert(Foo.aStaticFunction()); // alerts 'a static value'
var f = new Foo();
alert(f.aPrototypeValue); // alerts 'a prototype value'
f.aPrototypeFunction();
于 2012-03-05T11:59:50.313 に答える