From 92d3258bdf0e0db65d45ae2cb2e97310a85319c1 Mon Sep 17 00:00:00 2001 From: alattalatta Date: Fri, 17 Sep 2021 13:50:05 +0900 Subject: Rename html to md --- .../javascript/reference/functions/get/index.html | 177 --------------------- 1 file changed, 177 deletions(-) delete mode 100644 files/ko/web/javascript/reference/functions/get/index.html (limited to 'files/ko/web/javascript/reference/functions/get/index.html') diff --git a/files/ko/web/javascript/reference/functions/get/index.html b/files/ko/web/javascript/reference/functions/get/index.html deleted file mode 100644 index 55d7765229..0000000000 --- a/files/ko/web/javascript/reference/functions/get/index.html +++ /dev/null @@ -1,177 +0,0 @@ ---- -title: getter -slug: Web/JavaScript/Reference/Functions/get -tags: - - 자바스크립트 - - 함수 -translation_of: Web/JavaScript/Reference/Functions/get ---- -
{{jsSidebar("Functions")}}
- -

get 구문은 객체의 프로퍼티를 그 프로퍼티를 가져올 때 호출되는 함수로 바인딩합니다.

- -

{{EmbedInteractiveExample("pages/js/functions-getter.html")}}

- - - -

구문

- -
{get prop() { ... } } {get [expression]() { ... } }
- -

매개변수

- -
-
prop
-
주어진 함수를 바인딩할 프로퍼티의 이름입니다.
-
expression
-
ECMAScript 2015가 도입되면서, 함수의 이름을 계산하기 위해 표현식을 사용할 수 있습니다.
-
- -

설명

- -

어떤 프로퍼티에 접근할 때마다 그 값을 계산하도록 해야 하거나, 내부 변수의 상태를 명시적인 함수 호출 없이 보여주고 싶을 때, JavaScript의 getter를 이용할 수 있습니다. getter가 바인딩된 프로퍼티는 동시에 실제 값을 가질 수는 없지만, getter와 setter를 동시에 바인딩해 일종의 유사 프로퍼티(pseudo-property)를 만들 수는 있습니다.

- -

get 구문을 이용할 때는 다음을 유의하세요.

- -
- -
- -

getter는 delete 연산자를 이용해 삭제할 수 있습니다.

- -

- -

getter를 객체 초기자에서 정의하기

- -

객체 obj에 유사 프로퍼티 latest를 생성합니다. latest는 배열 log의 마지막 요소를 반환합니다.

- -
var obj = {
-  log: ['example','test'],
-  get latest() {
-    if (this.log.length == 0) return undefined;
-    return this.log[this.log.length - 1];
-  }
-}
-console.log(obj.latest); // "test".
- -

latest에 어떤 값을 할당하려고 시도해도 그 값이 바뀌지 않는다는 점을 유의하세요.

- -

delete연산자를 이용해 getter 삭제하기

- -

getter를 삭제하고 싶다면, delete를 이용하면 됩니다.

- -
delete obj.latest;
-
- -

defineProperty를 이용해 이미 존재하는 객체에 getter 정의하기

- -

이미 존재하는 객체에 getter를 추가하고자 한다면, {{jsxref("Object.defineProperty()")}}를 이용하면 됩니다.

- -
var o = { a:0 }
-
-Object.defineProperty(o, "b", { get: function () { return this.a + 1; } });
-
-console.log(o.b) // getter를 실행합니다. a + 1 (= 1)이 반환됩니다.
- -

계산된 (computed) 프로퍼티 이름

- -
var expr = "foo";
-
-var obj = {
-  get [expr]() { return "bar"; }
-};
-
-console.log(obj.foo); // "bar"
- -

똑똑한(Smart) / 스스로 덮어쓰는(self-overwriting) / 느긋한(lazy) getter

- -

Getter는 객체에 프로퍼티를 정의할 수 있도록 하지만, 그 프로퍼티에 접근하기 전까지는 값을 계산하지 않습니다. getter는 값을 계산하는 비용을 실제 값이 필요할 때까지 미루며, 그 값이 필요없다면 계산 비용도 들지 않습니다.

- -

또다른 최적화 기법으로는 계산 미루기와 함께 프로퍼티 값을 나중에 접근하기 위해 캐싱하는 것이 있습니다. 이를 똑똑한(smart), 혹은 메모화된(memoized) getter라고 부릅니다. 값은 getter가 호출될 때 처음 계산되며, 캐싱됩니다. 이후의 호출에는 다시 계산하지 않고 이 캐시값을 반환합니다. 이는 다음과 같은 상황에 유용합니다.

- - - -

똑똑한 getter는 값을 다시 계산하지 않기 때문에, 값의 변경이 예상되는 경우에는 똑똑한 getter를 이용해서는 안 됩니다.

- -

다음 예제에서, 객체는 원래 프로퍼티로 getter를 가집니다. 프로퍼티를 가져올 때, getter는 삭제되며 대신 명시적인 값이 저장됩니다. 최종적으로 값을 반환합니다.

- -
get notifier() {
-  delete this.notifier;
-  return this.notifier = document.getElementById("bookmarked-notification-anchor");
-},
- -

Firefox 코드의 경우,  defineLazyGetter() 함수를 정의하고 있는 XPCOMUtils.jsm 모듈을 참조하세요.

- -

get Vs. defineProperty

- -

While using the get 키워드와 {{jsxref("Object.defineProperty()")}}를 사용하면 비슷한 결과를 얻지만, {{jsxref("classes")}}에서 사용되는 두 가지 경우에는 미묘한 차이가 있습니다.

- -

get을 사용할 때 속성은 {{jsxref("Object.defineProperty()")}} 를 사용하는 동안 객체의 프로토 타입에 정의 될 것이고, 속성은 그것이 적용되는 인스턴스에 정의 될 것입니다.

- -
class Example {
-  get hello() {
-    return 'world';
-  }
-}
-
-const obj = new Example();
-console.log(obj.hello);
-// "world"
-console.log(Object.getOwnPropertyDescriptor(obj, 'hello'));
-// undefined
-console.log(Object.getOwnPropertyDescriptor(Object.getPrototypeOf(obj), 'hello'));
-// { configurable: true, enumerable: false, get: function get hello() { return 'world'; }, set: undefined }
- -

 

- -

스펙

- - - - - - - - - - - - - - - - - - - - - - - - -
스펙상태설명
{{SpecName('ES5.1', '#sec-11.1.5', 'Object Initializer')}}{{Spec2('ES5.1')}}최초로 정의되었습니다.
{{SpecName('ES6', '#sec-method-definitions', 'Method definitions')}}{{Spec2('ES6')}}계산된 프로퍼티 이름이 추가되었습니다.
{{SpecName('ESDraft', '#sec-method-definitions', 'Method definitions')}}{{Spec2('ESDraft')}} 
- -

브라우저 호환성

- -

{{Compat("javascript.functions.get")}}

- -

 

- -

참조

- - -- cgit v1.2.3-54-g00ecf