JavaScript set()

0
10K

In JavaScript, a setter can be used to execute a function whenever a specified property is attempted to be changed. Setters are most often used in conjunction with getters to create a type of pseudo-property. It is not possible to simultaneously have a setter on a property that holds an actual value.

Examples

 

Defining a setter on new objects in object initializers

The following example define a pseudo-property current of object language. When current is assigned a value, it updates log with that value:

const language = {
  set current(name) {
    this.log.push(name);
  },
  log: [],
};

language.current = "EN";
console.log(language.log); // ['EN']

language.current = "FA";
console.log(language.log); // ['EN', 'FA']

Note that current is not defined, and any attempts to access it will result in undefined.

Site içinde arama yapın
Kategoriler
Read More
Business
What Are the Components of a Good Business Plan?
A well-crafted business plan serves as a blueprint for your company’s success, whether...
By Dacey Rankins 2024-12-17 14:41:35 0 10K
Programming
Python Hello World Easter Egg
Python has gotten another easter egg from the module __hello__ or __phello__. When importing that...
By Jesse Thomas 2023-04-14 20:11:18 0 11K
Money
Bernard Madoff
Bernard Lawrence Madoff (born Bernard Lawrence Madoff; April 29, 1938 – April 14, 2021) was...
By Dacey Rankins 2024-10-22 17:01:55 0 13K
FAQs, Help, and Tutorials
215 Stop Words That Can Cause Your Email to Go to Spam
215 Stop Words That Can Cause Your Email to Go to Spam It is...
By Leonard Pokrovski 2024-03-26 20:59:12 0 32K
By Region
25 OF THE MOST POPULAR SOUVENIRS
Returning from a trip and choosing souvenirs, people tend to buy something special and unique,...
By FWhoop Xelqua 2022-12-17 14:47:00 0 22K

BigMoney.VIP Powered by Hosting Pokrov