TypeScript에서 가져 오기 및 설정


657

속성에 대한 get 및 set 메소드를 작성하려고합니다.

private _name: string;

Name() {
    get:
    {
        return this._name;
    }
    set:
    {
        this._name = ???;
    }
}

값을 설정하는 키워드는 무엇입니까?


12
밑줄 및 PascalCase는 Typescript 코딩 지침과 충돌합니다 : github.com/Microsoft/TypeScript/wiki/Coding-guidelines
Niels Steenbeek

2
안녕하세요 @NielsSteenbeek-TypeScript 컨트 리뷰 터 지침에 따라 속성 및 백업 필드를 사용하면 이름이 충돌하게됩니다. 제안 된 접근법은 무엇입니까?
Jordan

아마 : typescript private name: string; getName() { get: { return this.name; } set: { this.name = ???; } }
요르단

7
이러한 Typescript 코딩 지침은 매우 매력적이지 않습니다. 나는 강제적 인 상황에서만 그것들을 사용할 것입니다 (예 : 돈을 지불했습니다).
토마스 에딩

14
@NielsSteenbeek : 그 문서를 읽었습니까? "이것은 TypeScript 커뮤니티를위한 규범적인 지침이 아닙니다"
Jonathan Cast

답변:


1083

TypeScript는 ActionScript3와 같은 getter / setter 구문을 사용합니다.

class foo {
    private _bar: boolean = false;
    get bar(): boolean {
        return this._bar;
    }
    set bar(value: boolean) {
        this._bar = value;
    }
}

ECMAScript 5 Object.defineProperty()기능을 사용하여이 JavaScript를 생성 합니다.

var foo = (function () {
    function foo() {
        this._bar = false;
    }
    Object.defineProperty(foo.prototype, "bar", {
        get: function () {
            return this._bar;
        },
        set: function (value) {
            this._bar = value;
        },
        enumerable: true,
        configurable: true
    });
    return foo;
})();

사용하려면

var myFoo = new foo();
if(myFoo.bar) {         // calls the getter
    myFoo.bar = false;  // calls the setter and passes false
}

그러나이를 사용하려면 TypeScript 컴파일러가 ECMAScript5를 대상으로해야합니다. 명령 행 컴파일러를 실행중인 경우 다음 --target과 같이 플래그를 사용 하십시오.

tsc --target ES5

Visual Studio를 사용하는 경우 TypeScriptCompile 빌드 도구의 구성에 플래그를 추가하도록 프로젝트 파일을 편집해야합니다. 여기서 볼 수 있습니다 :

@DanFromGermany이 아래에서 제안하는 것처럼 단순히 지역 속성을 읽고 쓰는 foo.bar = true경우 세터와 게터 쌍을 갖는 것은 과도합니다. 속성을 읽거나 쓸 때마다 로깅과 같은 작업을 수행해야하는 경우 언제든지 나중에 추가 할 수 있습니다.


59
좋은 대답입니다. 또한 C #과 달리 속성은 현재 TypeScript (v0.9.5)에서 가상화되지 않습니다. 파생 클래스에서 "get bar ()"를 구현하면 부모에서 "get bar ()"가 교체됩니다. 파생 된 접근 자에서 기본 클래스 접근자를 호출 할 수 없다는 의미가 포함됩니다. 이것은 속성의 경우에만 해당됩니다. 예상대로 메서드가 작동합니다. 여기에 SteveFenton의 답변보기 : stackoverflow.com/questions/13121431/…
David Cuccia

14
밑줄에 대해 약간 혼란 스럽습니다. Typescript Convention은 개인 변수에 밑줄을 사용하지 말라고합니까? 그러나이 경우에는 밑줄을 사용해야합니다. 그렇지 않으면 개인 및 공개 "바"사이에 충돌이 발생합니다.
Kokodoko

4
밑줄은 개인 재산에 대한 개인적인 선호를 사용합니다. 그러나 속성이 getter / setter 메서드와 다른 이름을 갖기를 원한다고 생각합니다.
Ezward

3
또는 myFoo.bar = true대신에 왜 사용합니까 ?? myFoo.bar(true);myFoo.setBar(true);
Daniel W.

6
@DanFromGermany 속성은 "get"및 "set"메소드 쌍에 대한 "구문 설탕"입니다. Microsoft는 Visual Basic에서 속성 개념을 시작하여 C # 및 VB.NET과 같은 .NET 언어로 전달했습니다. 예를 들어 속성 (C # 프로그래밍 안내서)을 참조하십시오 . 속성은 객체의 상태에 대한 액세스를 단순화하고 (내 의견으로는) "get / set"메소드 쌍을 처리해야하는 "노이즈"를 제거합니다. (또는 때때로 불변성이 요구되는 "get"메소드 만)
DavidRR

112

Ezward는 이미 좋은 답변을 제공했지만 의견 중 하나가 어떻게 사용되는지 묻습니다. 이 질문을 우연히 발견 한 나와 같은 사람들에게는 Typescript 웹 사이트의 getter 및 setter에 대한 공식 문서에 대한 링크가 있으면 도움이 될 것이라고 생각했습니다. 만든 다음 사용 예를 보여줍니다.

http://www.typescriptlang.org/docs/handbook/classes.html

특히, 익숙하지 않은 사람들을 위해 'get'이라는 단어를 getter에 대한 호출에 포함시키지 마십시오 (세터에 대해서도 비슷 함).

var myBar = myFoo.getBar(); // wrong    
var myBar = myFoo.get('bar');  // wrong

당신은 단순히 이것을해야합니다 :

var myBar = myFoo.bar;  // correct (get)
myFoo.bar = true;  // correct (set) (false is correct too obviously!)

다음과 같은 수업이 주어졌습니다.

class foo {
  private _bar:boolean = false;

  get bar():boolean {
    return this._bar;
  }
  set bar(theBar:boolean) {
    this._bar = theBar;
  }
}

개인 '_bar'속성에 대한 'bar'getter가 호출됩니다.


공개 클래스 수준의 var를 속성으로 바꾸고 싶을 때 바로 넣을 수있는 대체품입니까? 걱정하지 않아도됩니까? 다시 말해, 한 접근 자와 하나의 세터를 회귀 테스트하면 성공으로 간주 될 수 있습니까? 또는 var와 정확히 동일하게 작동하지 않는 경우가 있으며이 var / prop를 사용하는 100 곳을 모두 테스트해야합니까?
Adam Plocher

속성 이름을 getter 또는 setter 메소드와 구별하기 위해 밑줄을 사용할 수있는 해결 방법이 있는지 궁금합니다. 한 과정에서 나는 밑줄이 바람직하지 않지만 대안을주지 않았다고 말했다.
cham

1
@cham 여기서 밑줄을 사용할 필요는 없습니다. 원하는 경우 개인 변수 notbar를 호출 할 수 있습니다.
Robert McKee

58

올바른 방향으로 안내하는 실제 예는 다음과 같습니다.

class Foo {
    _name;

    get Name() {
        return this._name;
    }

    set Name(val) {
        this._name = val;
    }
}

JavaScript의 Getter 및 Setter는 정상적인 기능입니다. 세터는 값이 설정되는 값의 매개 변수를 취하는 함수입니다.


30
명확하게하기 위해, getter 및 setter 속성이 필요하지 않습니다 static.
Drew Noakes

1
변수 참조는 여전히 정적입니다. Foo._name다음으로 교체해야합니다this._name
Johannes

6

당신은 이것을 쓸 수 있습니다

class Human {
    private firstName : string;
    private lastName : string;

    constructor (
        public FirstName?:string, 
        public LastName?:string) {

    }

    get FirstName() : string {
        console.log("Get FirstName : ", this.firstName);
        return this.firstName;
    }
    set FirstName(value : string) {
        console.log("Set FirstName : ", value);
        this.firstName = value;
    } 

    get LastName() : string {
        console.log("Get LastName : ", this.lastName);
        return this.lastName;
    }
    set LastName(value : string) {
        console.log("Set LastName : ", value);
        this.lastName = value;
    } 

}

2
왜 생성자에 공개해야합니까?
MuriloKunze

17
예,이 코드에서 생성자에 공개 할 수 없습니다. public여기서 중복 멤버를 정의합니다.
orad

2
쓸 수는 있지만 컴파일되지는 않습니다.
Justin

3

TS는 getter 및 setter를 제공하여 객체 속성 이 객체 외부 에서 액세스 (getter) 또는 업데이트 (setter)되는 방식을보다 잘 제어 할 수 있습니다 . 속성에 직접 액세스하거나 업데이트하는 대신 프록시 함수가 호출됩니다.

예:

class Person {
    constructor(name: string) {
        this._name = name;
    }

    private _name: string;

    get name() {
        return this._name;
    }

    // first checks the length of the name and then updates the name.
    set name(name: string) {
        if (name.length > 10) {
            throw new Error("Name has a max length of 10");
        }

        this._name = name;  
    }

    doStuff () {
        this._name = 'foofooooooofoooo';
    }


}

const person = new Person('Willem');

// doesn't throw error, setter function not called within the object method when this._name is changed
person.doStuff();  

// throws error because setter is called and name is longer than 10 characters
person.name = 'barbarbarbarbarbar';  

1

일반적인 방법을 만드는 것과 매우 유사합니다. 키워드를 예약 get또는 set시작 부분에 두기 만하면 됩니다.

class Name{
    private _name: string;

    getMethod(): string{
        return this._name;
    }

    setMethod(value: string){
        this._name = value
    }

    get getMethod1(): string{
        return this._name;
    }

    set setMethod1(value: string){
        this._name = value
    }
}

class HelloWorld {

    public static main(){

        let test = new Name();

        test.setMethod('test.getMethod() --- need ()');
            console.log(test.getMethod());

        test.setMethod1 = 'test.getMethod1 --- no need (), and used = for set ';
            console.log(test.getMethod1);
    }
}
HelloWorld.main();

이 경우 반환 유형을 건너 뛸 수 있습니다 get getMethod1() {

    get getMethod1() {
        return this._name;
    }

1

왜 그렇게 혼란 스러운지 알 것 같습니다. 귀하의 예에서에 대한 getter 및 setter를 원했습니다 _name. 그러나 관련이없는 클래스 변수에 대한 getter 및 setter를 작성하여이를 달성합니다 Name.

이걸 고려하세요:

class Car{
    private tiresCount = 4;
    get yourCarTiresCount(){
        return this.tiresCount ;
    }
    set yourCarTiresCount(count) {
        alert('You shouldn't change car tire count')
    }
}

위의 코드는 다음을 수행합니다.

  1. getset에 대한 getter 및 setter를 생성 yourCarTiresCount( 하지 않는tiresCount ).

게터는 :

function() {
    return this.tiresCount ;
}

세터는 다음과 같습니다.

function(count) {
    alert('You shouldn't change car tire count');
}

우리가 할 때마다 new Car().yourCarTiresCountgetter가 실행됩니다. 그리고 모든 new Car().yourCarTiresCount('7')세터 마다 실행됩니다.

  1. private에 대해 setter가 아닌 getter를 간접적으로 작성하십시오 tireCount.

0

클래스가 아닌 객체에서 get 및 set을 사용하는 방법을 찾고 있다면 Proxy 유용 할 수 있습니다 : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy

const target = {
  message1: "hello",
  message2: "everyone"
};

const handler3 = {
  get: function (target, prop, receiver) {
    if (prop === "message2") {
      return "world";
    }
    return Reflect.get(...arguments);
  },
};

const proxy3 = new Proxy(target, handler3);

console.log(proxy3.message1); // hello
console.log(proxy3.message2); // world

참고 : 이것은 새로운 API가 지원되지 않으며 구형 브라우저에 필요한 폴리 필이라는 점에 유의하십시오


-6

TypeScript 모듈로 작업 중이고 내 보낸 게터를 추가하려는 경우 다음과 같이 할 수 있습니다.

// dataStore.ts
export const myData: string = undefined;  // just for typing support
let _myData: string;  // for memoizing the getter results

Object.defineProperty(this, "myData", {
    get: (): string => {
        if (_myData === undefined) {
            _myData = "my data";  // pretend this took a long time
        }

        return _myData;
    },
});

그런 다음 다른 파일에 다음이 있습니다.

import * as dataStore from "./dataStore"
console.log(dataStore.myData); // "my data"

8
그것은 끔찍한 충고입니다. 특히 this모듈의 최상위 범위에서 정의되지 않아야합니다. exports대신 사용할 수는 있지만 호환성 문제가 발생할 수 있다는 보장이 있으므로 전혀 사용하지 마십시오.
Aluan Haddad
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.