2020-08-13(목) 영어단어 - Javascript Reflection and Reflect API

” JavaScript Reflection and Reflect API “



Reflect.deleteProperty(target,name)

예제 소스코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// /** start of Reflect.deleteProperty */

let car = {
    model: "Toyota Hilux",
    yearModel: 2020
};

//let us see the object before removing the model property.
console.log(car);  //output: {model: "Toyota Hilux", yearModel: 2020}

Reflect.deleteProperty(car, "model");

//let use the object after the removal of the model property.
console.log(car); //output: { yearModel: 2020 }

/** end of Reflect.deleteProperty */

출력결과

1
2
{model: "Toyota Hilux", yearModel: 2020}
{yearModel: 2020}

Reflect.deleteProperty(target, name)

1
Reflect.deleteProperty(car, "model");
  • target : first argument is the target/reference object
  • name : second argument is the name of the property that you want to be removed


Reflect.set(target, name, value)

예제 소스코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const computer2 = {
     processor: "Intel",
     brand: "Dell",
     operatingSystem: "windows 7"
};

console.log(computer2);
//output: {processor: "Intel", brand: "Dell", operatingSystem: "windows 7"}

Reflect.set(computer2, "processor", "AMD");

console.log(computer2);
//output: {processor: "AMD", brand: "Dell", operatingSystem: "windows 7"}

// /** end of Reflect.set */

출력결과

1
2
{processor: "Intel", brand: "Dell", operatingSystem: "windows 7"}
{processor: "AMD", brand: "Dell", operatingSystem: "windows 7"}

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

1
2
Reflect.set(computer2, "price", "27,000");
console.log(computer2);

출력결과 (추가된 내용)

1
{processor: "AMD", brand: "Dell", operatingSystem: "windows 7", price: "27,000"}

Reflect.set(target, name, value)

  • target : first argument is the target/reference object
  • name : second argument is the name of the object’s property
  • receiver : third argument is the value of the property



Reflect.get(target, name, receiver)

예제 소스코드 1

1
2
3
4
5
6
7
8
9
10
11
12
13
/** Start of Reflect.get */

var computer1 = {
    processor: "Intel",
    brand: "Dell",
    operatingSystem: "windows 7"
};

console.log(computer1);
Reflect.get(computer1, "processor");
console.log(computer1.processor);

/** end of Reflect.get */

출력결과 1

1
2
{processor: "Intel", brand: "Dell", operatingSystem: "windows 7"}
Intel
  1. Reflect.get(computer1, "processor");
    출력결과
1
"Intel"
  1. console.log(computer1); Reflect.get(computer1, "processor");
    출력결과
1
2
{processor: "Intel", brand: "Dell", operatingSystem: "windows 7"}
"Intel"
  1. Reflect.get(computer1, "processor"); console.log(computer1.processor);
1
Intel

예제 소스코드 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const dinoComputer = {
    processor: "Intel",
    brand: "Dell",
    operatingSystem: "windows 7"
};

Reflect.defineProperty(dinoComputer, "computerDetails", {
    get: function() {
        return new String().concat(`*********Computer Details********\r\n`,
                                   `****Processor: ${this.processor}***********\r\n`,
                                   `****Brand: ${this.brand}*****************\r\n`,
                                   `****Operating System: ${this.operatingSystem}*\r\n`);
    }
});

console.log(dinoComputer);

let oldComputer = Reflect.get(dinoComputer, "computerDetails",
{ processor: "AMD K62",
  brand: "Clone",
  operatingSystem: "Windows XP" });

console.log(oldComputer);

출력결과 2

1
2
3
4
5
{processor: "Intel", brand: "Dell", operatingSystem: "windows 7"}
*********Computer Details********
****Processor: AMD K62***********
****Brand: Clone*****************
****Operating System: Windows XP*

Reflect.get(target, name, receiver)

  • target : first argument is the target/reference object
  • name : second argument object’s property name
  • receiver : third argument is the receiver



링크

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