从JavascriptMVC中的静态方法获取静态属性的值
我正在和你一起做我的第一个项目 我有一门课从JavascriptMVC中的静态方法获取静态属性的值,javascript,model-view-controller,javascriptmvc,Javascript,Model View Controller,Javascriptmvc,我正在和你一起做我的第一个项目 我有一门课 $.Class('Foo',{ // Static properties and methods message: 'Hello World', getMessage: function() { return Foo.message; } },{}); 这个很好用。但是如果我不知道类名呢? 我想要这样的东西: $.Class('Foo',{ // Static properties and me
$.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.
但是如何在静态方法中做到这一点呢?事实是,我错了。可以在静态方法中使用它。 下面是一个小代码片段,可以帮助理解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();
但是你知道这个班的名字?!在这种情况下,我知道这一点,但我想编写一个带有一些静态方法的父类,并在继承的类中使用这些静态方法。因此,对于每个继承的方法,类名都会更改。我不想在每个继承的类中定义这些静态方法。