2017-10-07 12 views
1

클래스 내부에서 정적 함수를 어떻게 호출합니까? 나는이 대신 자기 키워드를 시도하지만 여전히 오류가 발생합니다.자체 클래스 내에서 정적 함수를 호출 할 수 없습니다.

class Test { 
 
    static staticFunction() { 
 
    console.log('Inside static function.'); 
 
    } 
 
    regularFunction() { 
 
    this.staticFunction(); 
 
    } 
 
} 
 

 
let test = new Test(); 
 
test.regularFunction();

답변

2

이 같이 클래스 이름을 통해 정적 함수를 참조 할 수 있습니다

class Test { 
 
    static staticFunction(input) { 
 
    console.log('Inside static function.'); 
 
    } 
 
    regularFunction() { 
 
    Test.staticFunction(); 
 
    } 
 
} 
 

 
let test = new Test(); 
 
test.regularFunction();

+0

위대한 @Patrick Hund, 마침내 해결 방법 :) – quarky

1

당신은 액세스에이 참조를 사용할 수 없습니다 정적 함수. staticFunction (input) 또는 더 나은 Test.staticFunction (input)을 수행해야합니다.) (

-1
`class Test { 
    static staticFunction(input) { 
     console.log('Inside static function.'); 
    } 
    regularFunction() { 
     Test.staticFunction("PassTheInputValueHere");//your correction is here 
    } 
    } 

    let test = new Test(); 
    test.regularFunction();` 

당신의 실수는 당신이 당신은 입력 값을 전달해야하는 정적 메소드를 호출하는 가정뿐만 아니라 입력 값을 전달하지 않았다이었다 당신은 'Classname.StaticMethod를 사용하여 정적 함수를 호출하기로 ; '

1

정적 메서드 나 클래스에서 'this'를 사용할 수 없습니다.