2012-03-05 2 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; 
    } 
},{}); 

을하지만 정적 속성에 을 사용할 수 없습니다. 그래서 정적 메서드에서 현재 클래스 이름을 가져올 수 있습니다. 프로토 타입 방법에서

그것은 간단하다

this.constructor.shortName/fullName. 

하지만 어떻게 정적 방법으로 그것을 할?

+0

하지만 당신은 클래스 이름을 알고 ... **?! ** – gdoron

+0

이 상황을 내가 알고 있지만 좀 정적 인 방법과, 부모 클래스를 작성하고,이 정적 메소드를 사용하려면에서 상속 된 클래스. 그래서 모든 상속 된 메소드에 대해 클래스 이름이 변경 될 것입니다. 모든 상속 된 클래스에서 이러한 정적 메서드를 정의하고 싶지 않습니다. –

답변

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