The Wayback Machine - https://web.archive.org/web/20241005011609/https://www.geeksforgeeks.org/javascript-reflect/
Open In App

JavaScript Reflect

Last Updated : 11 Apr, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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, "GeeksforGeeks");
  
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', 'GeeksforGeeks']
true
(4) ['Hello', 'Welcome', 'to', 'GeeksforGeeks']

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.



Similar Reads

JavaScript Reflect apply() Method
Javascript Reflect.apply() method is a standard build-in object in JavaScript which is used to call a function using the specified argument. It works similar to the Function.prototype.apply() method to call a function, but in an efficient manner and easy to understand. Syntax: Reflect.apply(target, thisArgument, argumentsList) Parameters: This meth
2 min read
JavaScript Reflect defineProperty() Method
JavaScript Reflect.defineProperty() method in JavaScript is used to allow the precise addition to or modification of a property on an object. This method returns a Boolean value which indicates whether the property was successfully defined. Syntax: Reflect.defineProperty(target, propertyKey, attributes) Parameter: This method accepts three paramete
2 min read
JavaScript Reflect get() Method
JavaScript Reflect.get() method in JavaScript is used to allow users to get the property from an object as a function. This method always returns the value of the property. Syntax: Reflect.get(target, propertyKey, receiver) Parameters: This method accepts three parameters as mentioned above and described below: target: This parameter is used to get
2 min read
JavaScript Reflect deleteProperty() Method
JavaScript Reflect.deleteProperty() method in JavaScript is used to delete a property on an object. It returns a Boolean value which indicates whether the property was successfully deleted. Syntax: Reflect.deleteProperty( target, propertyKey ) Parameters: This method accepts two parameters as mentioned above and described below: target: This parame
2 min read
JavaScript Reflect getPrototypeOf() Method
JavaScript Reflect.getPrototypeOf() method in JavaScript is used to return the prototype of the specified object. Syntax: Reflect.getPrototypeOf( obj ) Parameters: This method accepts a single parameter as mentioned above and described below: obj: This parameter is the target object and it is used to get the prototype.Return value: This method is u
2 min read
JavaScript Reflect has() Method
JavaScript Reflect.has() method in JavaScript is used to check whether the property exists in an object or not. It works like the in operator as a function. Syntax: Reflect.has(target, propertyKey) Parameters: This method accepts two parameters as mentioned above and described below: target: This parameter is the target object and it looks for the
2 min read
JavaScript Reflect isExtensible() Method
JavaScript Reflect.isExtensible() method in JavaScript is used to check whether an object is extensible or not(Checks whether other properties can be added to it or not). This method is similar to Object.isExtensible() but it will cause a TypeError if the target is not an object. Syntax: Reflect.isExtensible( obj )Parameters: This method accepts a
2 min read
JavaScript Reflect getOwnPropertyDescriptor() Method
JavaScript Reflect.getOwnPropertyDescriptor() method in Javascript is used to get the descriptor of an object if it exists in the object. It is the same as the Object.getOwnPropertyDescriptor method, but non-object targets are handled differently. Syntax: Reflect.getOwnPropertyDescriptor(obj, Key) Parameters: This method accepts two parameters as m
2 min read
JavaScript Reflect ownKeys() Method
JaScript Reflect.ownKeys() method in Javascript is used to return an array of the target object's own property keys and it ignores the inherited properties. Syntax: Reflect.ownKeys( obj ) Parameters: This method accepts a single parameter as mentioned above and described below: Obj: This parameter holds the target object and it is used to get its o
2 min read
JavaScript Reflect setPrototypeOf() Method
JavaScript Reflect.setPrototypeOf() method in JavaScript is used to set the prototype of a specified object to another object or to Null. This method returns the boolean value for the operation that is successful. This method is the same as the Object.setPrototypeOf() method. Syntax: Reflect.setPrototypeOf(obj, prototype) Parameters: Obj: This para
2 min read
JavaScript Reflect set() Method
JavaScript Reflect.set() method in JavaScript is used to set the value of an object property. It returns a Boolean value true if the property is successfully set else it returns false. Syntax: Reflect.set(obj, Key, value, receiver) Parameters: This method accepts four parameters as mentioned above and described below: Obj: This parameter holds the
2 min read
JavaScript Reflect Reference
JavaScript Reflect is a built-in object. It gives access to other methods for interceptable operations. Like most objects in JavaScript, it is not a constructor. Syntax: Reflect.function()Example: Below examples illustrate the Reflect.setPrototypeOf() method in JavaScript: C/C++ Code const object1 = {}; console.log(Reflect.setPrototypeOf(object1, O
2 min read
JavaScript Reflect Methods
Reflect is a built-in JavaScript object which gives access to other methods for interceptable operations. JavaScript Reflect methods are the same as those of proxy handlers and are static just like the Math object. As it is not a JavaScript function object, so it's not constructible. It cannot be used with the JavaScript new operator and cannot be
3 min read
What is the Reflect Object in JavaScript ?
The Reflect object in JavaScript provides methods for interceptable JavaScript operations and direct manipulation of objects. JavaScript Reflect object provides the following static methods. Reflect.apply(target, thisArgument, argumentsList)Reflect.construct(target, argumentsList[, newTarget])Reflect.defineProperty(target, propertyKey, attributes)R
1 min read
JavaScript Reflect construct() Method
JavaScript Reflect.construct() method in JavaScript is used to call a new target. It gives also the added option to specify a different prototype. Syntax: Reflect.construct(para1, para2, para3)Parameters: This method accepts three parameters as mentioned above and described below: para1: This parameter is the target function that is going to be cal
2 min read
JavaScript Reflect preventExtensions() Method
JavaScript Reflect.preventExtensions() method in JavaScript is used to prevent future extensions to the object which means preventing from adding new properties to the object. Syntax: Reflect.preventExtensions( obj ) Parameters: This method accepts a single parameter as mentioned above and described below: Obj: This parameter holds the target objec
2 min read
JavaScript Course Understanding Code Structure in JavaScript
Inserting JavaScript into a webpage is much like inserting any other HTML content. The tags used to add JavaScript in HTML are <script> and </script>. The code surrounded by the <script> and </script> tags is called a script blog. The 'type' attribute was the most important attribute of <script> tag. However, it is no
4 min read
Introduction to JavaScript Course - Learn how to build a task tracker using JavaScript
This is an introductory course about JavaScript that will help you learn about the basics of javascript, to begin with, the dynamic part of web development. You will learn the basics like understanding code structures, loops, objects, etc. What this course is about? In this course we will teach you about the basics of the scripting language i.e) Ja
4 min read
JavaScript Course Loops in JavaScript
Looping in programming languages is a feature that facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true. For example, suppose we want to print “Hello World” 10 times. Example: In this example we will print the same things, again and again, to understand the work of Loops. <script> cons
4 min read
JavaScript Course Logical Operators in JavaScript
logical operator is mostly used to make decisions based on conditions specified for the statements. It can also be used to manipulate a boolean or set termination conditions for loops. There are three types of logical operators in Javascript: !(NOT): Converts operator to boolean and returns flipped value &&:(AND) Evaluates operands and retu
3 min read
JavaScript Course What is JavaScript ?
JavaScript is a very famous programming language that was originally started in the year of 1995 with the motive of making web pages alive. It is also an essential part of the web developer skillset. In simple terms, if you want to learn web development then learn HTML & CSS before starting JavaScript. HTML: HTML is used to create the structure of
3 min read
JavaScript Course Operators in JavaScript
An operator is capable of manipulating a certain value or operand. Operators are used to performing specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands. In JavaScript, operators are used for comparing values, performing arithmetic operations, etc. There are various operators
7 min read
JavaScript Course Functions in JavaScript
Javascript functions are code blocks that are mainly used to perform a particular function. We can execute a function as many times as we want by calling it(invoking it). Function Structure: To create a function, we use function() declaration. // Anonymous function function(){ // function...body } // function with a name function displayMessage(){
4 min read
JavaScript Course Conditional Operator in JavaScript
JavaScript Conditional Operators allow us to perform different types of actions according to different conditions. We make use of the 'if' statement. if(expression){ do this; } The above argument named 'expression' is basically a condition that we pass into the 'if' and if it returns 'true' then the code block inside it will be executed otherwise n
3 min read
JavaScript Course Objects in JavaScript
We have learned about different Data Types that javascript provides us, most of them primitive in nature. Objects are not primitive in nature and are a bit complex to understand. Everything in javascript is basically an object, and that is the reason why it becomes very important to have a good understanding of what they are. Objects are used to st
4 min read
How to compare two JavaScript array objects using jQuery/JavaScript ?
In this article, we are given two JavaScript array/array objects and the task is to compare the equality of both array objects. These are the methods to compare two JavaScript array objects: Using jQuery not() methodUse the sort() functionUse JSON.stringify() functionUsing every() and indexOf()Using Lodash _.isEqual() MethodApproach 1: Using jQuery
3 min read
How can JavaScript codes be hidden from old browsers that do not support JavaScript ?
Nowadays all modern browsers support JavaScript, however, older browsers did not support JavaScript. In this article, we will learn how we can hide JavaScript code from running in older browsers. Approach: After opening the <script> tag, we will use a one-line HTML style comment without any closing character ( <!-- ). We will then write th
2 min read
How are the JavaScript window and JavaScript document different from one another?
What is a JavaScript window? The window is at a root/top level at the JavaScript object hierarchy. It is a global/root object in JavaScript and it is the root object of the Document object model(DOM); What is a JavaScript document? A document is an object inside the window object and we use a document object for manipulation inside the document. Th
3 min read
Explore the concept of JavaScript Function Scope and different types of JavaScript Functions
JavaScript is based on functional programming. Therefore, functions are fundamental building blocks of JavaScript. So the function is called first-class citizens in JavaScript. Syntax: Define a functionfunction functionName(parameters) { // SET OF STATEMENTS } Call a functionfunctionName(arguments); The function execution stops when all the stateme
10 min read
Which href value should we use for JavaScript links, "#" or "javascript:void(0)" ?
In this article, we will know which "href" should be used for JavaScript when we can use "#" or "javascript:void(0)". Let us understand it. Using the '#' value: The # is a fragment identifier used to refer to a specific section of the same document. When a user clicks a link with a # href, the browser will scroll to that section of the page specifi
3 min read
Article Tags :