How to Use Proxies, Functions, and Built-in Objects in Object-Oriented JavaScript?

Estimated read time 1 min read

In object-oriented JavaScript, you can use proxies, functions, and built-in objects to add additional functionality to objects.

Examples:

Using Proxies:

const person = { name: "John Doe", age: 30 };
const handler = {
  get: function(target, property) {
    if (property in target) {
      return target[property];
    } else {
      return "Property not found.";
    }
  }
};
const proxy = new Proxy(person, handler);
console.log(proxy.name);  // "John Doe"
console.log(proxy.email);  // "Property not found."

Using Functions:

const person = { name: "John Doe", age: 30 };
const sayHello = function() {
  console.log(`Hello, I am ${this.name}.`);
};
sayHello.call(person);  // "Hello, I am John Doe."

Using Built-in Objects:

const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((acc, cur) => acc + cur);
console.log(sum);  // 15

// or

const name = "John Doe";
const reversed = Array.from(name).reverse().join("");
console.log(reversed);  // "eoD nhoJ"

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply