HomeLanguagesJavascriptJavaScript Reflect

JavaScript Reflect

JavaScript Reflect is a built-in object that gives access to other elements for interceptable operations. This object can be used to check whether an object has a particular property or to change or add new properties to an existing object. This object cannot be explicitly created using the new keyword and we cannot call it a function. Reflect has a set of static functions to perform operations.

Syntax:

Reflect.staticFunc()

Parameters: This object does not have fix parameters, it depends upon the static function being used with it

Return Type: The return values depend on the function being used.

Example 1: This example uses Reflect method to check and add properties in a particular object.

Javascript




var details = {
    name: "Raj",
    course: "DSA",
    website: "geeksforgeeks.org",
}
  
console.log(Reflect.has(details, "course"))
  
Reflect.set(details, "Rating", "5");
  
console.log(details)


Output:

true
{name: 'Raj', course: 'DSA', website: 'geeksforgeeks.org', Rating: '5'}

Example 2: This example uses Reflect functions to construct a new object.

Javascript




class Details {
    constructor(name, course) {
        this.name = name;
        this.course = course;
    }
    get fullDetails() {
        return `${this.name} ${this.course}`;
    }
}
  
var person = ["Shobhit", "DSA"]
  
var enroll = Reflect.construct(
    Details,
    person
);
  
console.log(enroll instanceof Details);
console.log(enroll);


Output:

true
Details {name: 'Shobhit', course: 'DSA'}

Example 3: This example uses Reflect methods to freeze an array so that new elements cannot be added to it.

Javascript




var arr = [];
  
Reflect.set(arr, 0, "Hello");
Reflect.set(arr, 1, "Welcome");
Reflect.set(arr, 2, "to");
Reflect.set(arr, 3, "neveropen");
  
console.log(arr);
console.log(Reflect.isExtensible(arr))
  
Reflect.preventExtensions(arr);
  
Reflect.set(arr, 4, "DSA");
  
console.log(arr)


Output: Using the preventExtensions method new properties cannot be added to the array. This helps to freeze the array

(4) ['Hello', 'Welcome', 'to', 'neveropen']
true
(4) ['Hello', 'Welcome', 'to', 'neveropen']

Supported Browsers:

  • Chrome
  • Edge
  • Firefox
  • Opera
  • Safari

We have a complete list of JavaScript Reflect methods, to check please go through the JavaScript Reflect Reference article.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

4 COMMENTS

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS