2012-03-05 125 views
1

我正在使用JavascriptMVC处理我的第一个项目。从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; 
    } 
},{}); 

,但我不能在静态属性中使用这个。 那么,如何从静态方法获取当前类名。

从原型方法很简单:

​​

,但如何做到这一点的静态方法?

+0

但是,你知道的类名... **!** – gdoron 2012-03-05 10:14:58

+0

在这种情况下,我知道,但我想编写一个父类,有一些静态方法,并使用该静态方法继承的类。所以类名将会改变,对于每一个继承的方法。我不想在每个继承的类中定义这些静态方法。 – 2012-03-05 10:17:56

回答

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();