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 回答