offers tools to do things like create objects, arrays, etc during runtime

Creating Objects with Reflect.construct()


Example One - Creating a object with reflect (required arguments)

//ES6 Reflect API - Creating objects - the Optional third argument

class Daughter {
  constructor(name) {
    this.name = name;
  }
}

//Construct takes up to three arguments
// First Argument - constructor function to use for the class
// Second Argument - array which specifies what to pass through the constructor
// Third Argument (optional) - used to override the prototype

let daughter = Reflect.construct(Daughter, ['Annabelle']);
console.log(daughter)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Example Two - Creating a object with reflect (optional argument)

//ES6 Reflect API - Creating objects - the Optional third argument

class Daughter {
  constructor(name) {
    this.name = name;
  }
}

//Construct function we will use to overwrite the Daughter prototoype
function OverObj() {
  this.age = 2;
}

let daughter = Reflect.construct(Daughter, ['Annabelle'], OverObj);
console.log(daughter)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Calling Functions with Reflect.apply()


With Reflect.apply() we can control the context of 'this'

Example One - Using reflect apply

//ES6 Reflect API - Calling functions with reflect.apply()

class Daughter {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
  
  dance() {
    console.log(this.name + ', is dancing.')
  }
}

let daughter = Reflect.construct(Daughter, ['Annabelle', 2.5]);

//Call the dance function method #1
// daughter.dance()

/**
* Using Reflect apply()
*
* @{function} the method to call
* @{this} what to apply this to
*/

Reflect.apply(daughter.dance, daughter, []);

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

Example Two - Using reflect apply - changing the context of 'this'

//ES6 Reflect API - Calling functions with reflect.apply()

class Daughter {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
  
  dance() {
    console.log(this.name + ', is dancing.')
  }
}

let daughter = Reflect.construct(Daughter, ['Annabelle', 2.5]);

//We are able to apply this context
Reflect.apply(daughter.dance, {}, []); // Undefined is dancing
Reflect.apply(daughter.dance, {name : 'Violet'}, []); // Violet is dancing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Example Three - Using reflect apply - passing arguments to inner method

//ES6 Reflect API - Calling functions with reflect.apply()

class Daughter {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
  
  dance(danceSpeed) {
    console.log(this.name + ', is dancing ' + danceSpeed)
  }
}



let daughter = Reflect.construct(Daughter, ['Annabelle', 2.5]);


Reflect.apply(daughter.dance, daughter, []); // Annabelle is dancing undefined
Reflect.apply(daughter.dance, daughter, ['quickly']); // Annabelle is dancing quickly



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

Reflect and Prototypes


//ES6 Reflect API - Reflect and Prototypes

class Pet {
  constructor() {
    this.name = "Broccoli";
  }
  
}



let pet = new Pet();

//Get Prototype with reflect
console.log(Reflect.getPrototypeOf(pet));
console.log(Reflect.getPrototypeOf(pet) === Pet.prototype);

Pet.prototype.age = 4;

console.log(Reflect.getPrototypeOf(pet));

//Change prototype with reflect

let proto = {
  age : 8
}

Reflect.setPrototypeOf(pet, proto);
console.log(Reflect.getPrototypeOf(pet))
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

Reflect.construct(), apply() and Prototypes Interaction


//ES6 Reflect API - Reflect.construct(), apply(), and prototype interaction

class Pet {
  constructor() {
    this.name = "Broccoli";
  }
}

let pet = new Pet();

Pet.prototype.age = 4;

let proto = {
  age : 8,
  eat() {
    console.log('Chewing');
  }
}

Reflect.setPrototypeOf(pet, proto);

//Apply
Reflect.apply(pet.eat, null, [])
console.log(Reflect.getPrototypeOf(pet))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

Accessing Properties with Reflect


Analyzing Objects with Reflect.ownKeys()


Creating & Deleting Properties with Reflect


Preventing Object Extensions & Wrap Up


Resources


Last Updated: 8/11/2019, 10:51:29 PM