
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
What are JavaScript Classes and Proxies?
In this article, we are going to explore Classes and Proxies and the difference between the two.
Classes in JavaScript are similar to functions. The only difference is it uses the class keyword instead of the function. Another important difference between the functions and the classes is that the functions can be called in the code even before they are defined. Whereas the class object should be defined before the class object to prevent it from throwing any Reference error.
Syntax
class Classname { constructor(property1, property2) { this.property1 = property1; this.prop erty2 = property2; } }
Example 1
In this example, we are going to use the methods from the weak set and see how to use it in various cases.
#Filename: index.html
<!DOCTYPE html> <html lang="en"> <head> <title>Classes and Proxies</title> </head> <body> <h1 style="color: red;"> Welcome To Tutorials Point </h1> <script> class Student { constructor(name, id) { this.name = name; this.id = id; } } let student = new Student("Steve", 101); console.log(student); </script> </body> </html>
Output
It will produce the following output in the Console.
Student {name: 'Steve', id: 101}
Class Expressions − We can also define a class using the class expressions. Class expressions can be of two types namely: named and unnamed. The name of the class can be accessed by the name property.
Syntax
let Employee = class { constructor(name, id) { this.name = name; this.id = id; } }
Example 2
#Filename: index.html
<!DOCTYPE html> <html lang="en"> <head> <title>Classes and Proxies</title> </head> <body> <h1 style="color: red;"> Welcome To Tutorials Point </h1> <script> // Unnamed class let student = class { constructor(name, id) { this.name = name; this.id = id; } } console.log(student.name); // Named class let studentV2 = class junior { constructor(name, id) { this.name = name; this.id = id; } } console.log(studentV2.name); </script> </body> </html>
Output
It will produce the following output in the Console.
student 27 junior
Proxies − These are the objects that are used for refining the fundamental operations of the object. It allows the user to create a proxy of another object.
Parameters
The proxy accepts two parameters that are −
target − This is the original object that is required for wrapping the proxy.
handler − This object property defines the proxy behavior 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 3
<!DOCTYPE html> <html lang="en"> <head> <title>Classes and Proxies</title> </head> <body> <h1 style="color: red;"> Welcome To Tutorials Point </h1> <script> const target = { property1: "Tutorials Point", property2: "SIMPLY LEARNING" }; const handler = { get: function (target, prop, receiver) { if (prop === "property2") { return "Start your Learning journey today !"; } else { return Reflect.get(target,prop); } } } const proxy = new Proxy(target, handler); console.log(proxy.property1); console.log(proxy.property2); </script> </body> </html>
Output
It will produce the following output in the Console.
Tutorials Point Start your Learning journey today !
- Related Articles
- What are Java classes?
- What are Base and Derived Classes in C#?
- What are CSS pseudo-classes
- What are Java parent and child classes in Java?
- What are the main classes and interfaces of JDBC?
- Getters and setters in JavaScript classes?
- What are final classes in Java?
- What are abstract classes in Java?
- What are inner classes in Java?
- What are wrapper classes in Java?
- What are abstract classes in C#?
- What are the classes in C#?
- What are collection classes in C#?
- What are nested classes in C#?
- What are the differences between Java classes and Java objects?
