2016-12-20 2 views
1

이미 this question을 체크 아웃했으나 이에 대한 대답이 정확하지 않은 것에 유의하십시오.Typescript의 전용 함수

내가 (답변 할 수없는 제안) 일반 자바 스크립트에서 개인 방법을 가지고 싶었다면

, 나는 같은 것을 할 것 : 나는 구문이 무엇인지 알아 내려고 노력하고있어

 
function myThing() { 
    var thingConstructor = function(someParam) { 
     this.publicFn(foo) { 
      return privateFn(foo); 
     } 
     function privateFn(foo) { 
      return 'called private fn with ' + foo; 
     } 
    } 
} 

var theThing = new myThing('param'); 
var result = theThing.publicFn('Hi');//should be 'called private fn with Hi' 
result = theThing.privateFn; //should error 

을 TypeScript에서 private 함수를 캡슐화합니다. 그럴 수 없다는 것을 알았다면 괜찮습니다. 그러나 오래된 질문의 답은 일반 JavaScript에서 개인 메서드를 만들 수 없다고 잘못 말하면서 대답을 권위있는 것으로 받아들이지 않습니다.

답변

0

그래서 개인용으로 표시하는 것처럼 간단합니다. 누락 된 것은 this 키워드를 사용해야한다는 것입니다.

그래서

 
export class myThing { 
    constructor(){} 
    publicFn(foo) { 
     return this.privateFn(foo); 
    } 
    private privateFn(foo) { 
     return 'called private fn with ' + foo; 
    } 
}