What are the classes and proxies in JavaScript ?
Last Updated :
18 Aug, 2022
Classes: These are almost similar to functions, except they use a class keyword instead of a function keyword. Another important difference between functions and classes is that functions can be called in code before they are defined whereas classes must be defined before a class object is constructed in JavaScript, else running the code will throw a Reference error.
Defining a class: A class can be defined by using a class keyword along with a constructor function to initialize it.
Syntax:
class Classname {
constructor(property1, property2) {
this.property1 = property1;
this.property2 = property2;
}
}
Example: The following example describes for a simple classes.
JavaScript
<script>
class Employee {
constructor(name, id) {
this.name = name;
this.id = id;
}
}
let Employee1 = new Employee("Suraj", 533);
console.log(Employee1);
</script>
Output:
Employee {name: 'Suraj', id: 533}
id: 533
name: "Suraj"
[[Prototype]]: Object
Class Expressions: We can also define a class using class expressions. They can be of two types namely "named" or "unnamed". The name of a class can be accessed by the name property.
Syntax:
let Employee = class {
constructor(name, id) {
this.name = name;
this.id = id;
}
}
Example:
JavaScript
<script>
// Unnamed class
let Employee1 = class {
constructor(name, id) {
this.name = name;
this.id = id;
}
}
console.log(Employee1.name);
// Named class
let Employee2 = class Intern {
constructor(name, id) {
this.name = name;
this.id = id;
}
}
console.log(Employee2.name);
</script>
Output:
Employee1
Intern
Class methods: We can also define methods inside classes. It can be with or without parameters.
Syntax:
class Classname {
constructor(property1, property2) {
this.property1 = property1;
this.property2 = property2;
}
method1() { ... }
}
Example:
JavaScript
<script>
class Employee {
constructor(name, id) {
this.name = name;
this.id = id;
}
// Without parameter
getname(){
return "Name of Employee: "+this.name;
}
// With parameter
getdept(department){
return "Works in " + department;
}
}
let Employee1 = new Employee("Suraj", 533);
console.log(Employee1.getname());
console.log(Employee1.getdept("Finance department"));
</script>
Output:
Name of Employee: Suraj
Works in Finance department
Proxies: Proxies are objects that are used to redefine the fundamental operations of an object. It allows us to create a proxy of another object.
Parameters:
A proxy object accepts two parameters described below:
- target: It is the original object of which we want to wrap with proxy.
- handler: An object whose properties define the behavior of proxy if an operation is performed on that proxy.
Syntax:
const target = {
property1: "first property",
property2: "second property"
};
const handler = { ... };
const proxy1 = new Proxy(target, handler);
Example:
JavaScript
<script>
const target = {
property1: "GeeksforGeeks",
property2: "Computer science portal"
};
const handler = {};
const proxy = new Proxy(target, handler);
console.log(proxy.property1);
console.log(proxy.property2);
</script>
Output:
GeeksforGeeks
Computer science portal
Since the handler is empty, it does not affect the target. Thus, the proxy behaves as its original target.
We can also define operations within the handler to change the properties of the proxy from its original target. For this, we need to use the get() handler which can access the properties of the target and manipulate the properties inside the proxy. The reflect class helps to apply the original properties of the target to the proxy.
Example: We applied if condition in the handler, to check for "property2" of the target, the output should be changed from the original property of the target.
JavaScript
<script>
const target = {
property1: "GeeksforGeeks",
property2: "Computer science portal"
};
const handler = {
get: function (target, prop, receiver) {
if (prop === "property2") {
return "For computer science geeks";
}
else{
return Reflect.get(target,prop);
}
}
}
const proxy = new Proxy(target, handler);
console.log(proxy.property1);
console.log(proxy.property2);
</script>
Output:
GeeksforGeeks
For computer science geeks
Similar Reads
Classes and Objects in JavaScript
Classes Classes were first introduced in the new version of the ES6 classes which replaced the previously used functions. Class is nothing but a blueprint for an object of it. It is used to create an object mainly. If we relate it to a real-life example then it is like a plan for a building or house
4 min read
What Are Access Modifiers In JavaScript ?
Access modifiers in JavaScript play a significant role in controlling the visibility and accessibility of class members. Although JavaScript is not traditionally an object-oriented programming (OOP) language, the introduction of classes and access modifiers in ECMAScript 6 (ES6) allowed developers t
4 min read
JavaScript Nested Classes
Let us try to understand, what is class. A class in JavaScript is a type of function, which can be initialized through both function keywords as well as through class keywords. In this article, we will cover the inner class in javascript through the use of a function keyword. Here's an example of th
3 min read
4 Ways to Make an API Call in JavaScript
API(Application Programming Interface) is a set of protocols, rules, and tools that allow different software applications to access allowed functionalities, and data, and interact with each other. API is a service created for user applications that request data or some functionality from servers.To
7 min read
How to call the constructor of a parent class in JavaScript ?
In this article, we learn how to call the constructor of a parent class. Before the beginning of this article, we should have a basic knowledge of javascript and some basic concepts of inheritance in javascript. Constructor: Constructors create instances of a class, which are commonly referred to as
4 min read
How to get the Class Name of an Object in JavaScript
In this article, we will learn how we can get the class Name of an Object with multiple approaches in JavaScript. In JavaScript, determining an object's class name can be done using multiple approaches. The constructor property of an object reveals the function that created it, while the instanceof
3 min read
What are the variable naming conventions in JavaScript ?
When we name variables in javascript, we've to follow certain rules. Each variable should have a name that appropriately identifies it. Your JavaScript code gets easier to comprehend and work with when you use suitable variable names. It's critical to name variables correctly. For example Constants
2 min read
How to create an object with prototype in JavaScript ?
In this article, we will discuss object creation & prototypes, along with understanding the different ways for object creation & their implementation through the examples. Prototypes are the mechanism by which objects in JavaScript inherit features from another object. A prototype property i
4 min read
What is the first class function in JavaScript ?
First-Class FunctionA programming language is said to have First-class functions if functions in that language are treated like other variables. So the functions can be assigned to any other variable or passed as an argument or can be returned by another function. JavaScript treats function as a fir
2 min read
What is First Class Citizen in JavaScript?
In JavaScript, a First Class Citizen is an entity that can be assigned to a variable, passed as an argument to a function, returned from a function, and has properties and methods assigned to it. Functions are examples of First Class Citizens in JavaScript. Below are some terms related to First Clas
2 min read