JavaScript set()

0
11كيلو بايت

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.

البحث
الأقسام
إقرأ المزيد
Business
Why is a Business Plan Important?
A business plan is more than just a formal document; it's the blueprint for a company's growth,...
بواسطة Dacey Rankins 2024-12-18 14:26:23 0 10كيلو بايت
Marketing and Advertising
Who Is the Target Audience, and What Role Do They Play in Positioning?
Introduction: The Heart of Every Brand Strategy No matter how innovative your product or...
بواسطة Dacey Rankins 2025-10-23 15:37:44 0 3كيلو بايت
Finance
What Time Frame Should I Use in Forex?
What Time Frame Should I Use in Forex? Trading in the foreign exchange (Forex) market involves...
بواسطة Leonard Pokrovski 2025-10-15 11:14:16 0 3كيلو بايت
Programming
Python UnicodeError
In Python, Unicode standards have two types of error: Unicode encodes error and Unicode decode...
بواسطة Jesse Thomas 2023-03-31 20:38:35 0 12كيلو بايت
Social Issues
Double Indemnity. (1944)
A Los Angeles insurance representative lets an alluring housewife seduce him into a scheme of...
بواسطة Leonard Pokrovski 2023-01-21 21:57:47 0 25كيلو بايت

BigMoney.VIP Powered by Hosting Pokrov