2020-08-12(수) 영어단어 - Javascript Reflection and Reflect API

” JavaScript Reflection and Reflect API “

  • equivalent : 동등한, -에 대응하는 것
  • identical : 동일한, 똑같은
  • either A or B : (둘 중)어느 하나의
  • It is false by default : 그것은 기본적으로 거짓이다
  • enumerable : 열거
  • retrieve : (정보를)검색하다, 되찾다, 회수하다

Reflect.defineProperty(target, name, desc)

  • 이 메소드는 새로운 속성을 정의하거나 존재하는 속성을 수정하는 것이다.

Object.defineProperty와 Reflect.defineProperty의 차이

  • 리턴값이 다르다.

    • Reflect.defineProperty()는 Boolean 타입으로 리턴
    • Object.defineProperty()는 수정한 객체로 리턴
  • 만약, 메소드들이 실패하였다면?

    • Reflect.defineProperty()는 false를 리턴
    • Object.defineProperty()는 예외를 발생시킨다.

data properties와 accessor properties의 차이

  • data properties : 읽을 수 있거나 읽을 수 없거나 쓸 수 있거나 쓸 수 없는 값을 가지고 있다.
  • accessor property : 속성 값을 설정하거나 되찾기 위한 함수의 getter-setter 쌍을 가지고 있다.

Using Reflect.define() and defining a data property descriptor

예제 소스코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const book = {};

//let's define a property that is writable, configurable and enumerable

Reflect.defineProperty(book, "title", {
    value: "JavaScript For Kids",
    writable: true,
    configurable: true,
    enumerable:true
});

//let's check the book object
console.log(book); //output: {title: "JavaScript For Kids"}

//let's check the title of the book
console.log(book.title); //output: JavaScript For Kids

//let's change the value of the Book property,
//this is possible because writable is set to true
book.title = "Beginning Node.js";

//let's check the title of the book
console.log(book.title); //output: Beginning Node.js

//let's check if we can enumerate the title property

for (const key in book) {
    console.log(key); //output: title
}

출력결과

1
2
3
4
{title: "JavaScript For Kids"}
JavaScript For Kids
Beginning Node.js
title

예제 소스코드 (내용 추가)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Reflect.defineProperty(book, "writer", {
    value: "Hong kil Dong",
    writable: true,
    configurable: true,
    enumerable:true
});

//let's check the book object
console.log(book); //output: {title: "JavaScript For Kids"}

//let's check the title of the book
console.log(book.writer); //output: JavaScript For Kids

//let's change the value of the Book property,
//this is possible because writable is set to true
book.writer = "Shin Yoon Jung";

//let's check the title of the book
console.log(book.writer); //output: Beginning Node.js

//let's check if we can enumerate the title property

for (const key in book) {
    console.log(key); //output: title
}

출력결과 (내용 추가)

1
2
3
4
5
{title: "Beginning Node.js", writer: "Hong kil Dong"}
Hong kil Dong
Shin Yoon Jung
title
writer

예제 소스코드 (false로 변경)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const laptop = {};

//let's define a property that isn't writable, configurable and enumerable

Reflect.defineProperty(laptop, "brand", {
    value: "IBM",
    writable: false,
    configurable: false,
    enumerable: false
});

//let's check the laptop object
console.log(laptop); //output: {brand: "IBM"}

//let's check the brand of the laptop
console.log(laptop.brand); //output: IBM

//let's change the value of the brand property,
//this is not possible because writable is set to false
laptop.brand = "DELL";

//let's check the brand of the laptop
console.log(laptop.brand); //output: IBM

//let's check if we can enumerate the brand property
for (const key in laptop) {
    console.log(key); //output: n/a
}

/** end of Reflect.defineProperty */

출력결과 (false로 변경)

1
2
3
{brand: "IBM"}
IBM
IBM

Using Reflect.define() and defining an accessor property descriptor

예제 소스코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/** start of accessor property */
/** start of Reflect.defineProperty */

const laundryShop = {
    __defaultName__: "Queens Care Laundry Shop"
}

Reflect.defineProperty(laundryShop, "businessName", {
    get: function () {
        return this.__defaultName__;
    },
    set: function (value){
        this.__defaultName__ = value;
    },
    configurable: true,
    enumerable: true
});

console.log(laundryShop); //output: {__defaultName__: "Queens Care Laundry Shop"}
console.log(laundryShop.businessName); //output: Queens Care Laundry Shop
laundryShop.businessName = "Laundry Shop";
console.log(laundryShop.businessName); //output: Laundry Shop

/** end of accessor property */
/** end of Reflect.defineProperty */

출력결과

1
2
3
{__defaultName__: "Queens Care Laundry Shop"}
Queens Care Laundry Shop
Laundry Shop

링크

https://www.codeproject.com/Articles/5275933/JavaScript-Reflection-and-Reflect-API