JavaScript set()

0
7كيلو بايت

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.

البحث
الأقسام
إقرأ المزيد
Crafts
Arts and Crafts
Arts & Crafts was an English art movement of the Victorian era (late nineteenth century),...
بواسطة FWhoop Xelqua 2023-07-01 19:15:47 0 18كيلو بايت
Научная фантастика и фэнтези
Безумный Макс: Дорога ярости. Mad Max: Fury Road. (2015)
Преследуемый призраками беспокойного прошлого Макс уверен, что лучший способ выжить - скитаться в...
بواسطة Nikolai Pokryshkin 2023-04-05 17:58:27 0 30كيلو بايت
Science Fiction and Fantasy
Mad Max: Fury Road. (2015)
In a post-apocalyptic wasteland, a woman rebels against a tyrannical ruler in search for her...
بواسطة Leonard Pokrovski 2023-04-06 19:58:58 0 25كيلو بايت
Issues
Problem
Problema (ancient Greek: Problema) πρόβλημα - task, task) in a...
بواسطة FWhoop Xelqua 2023-04-19 17:27:03 0 11كيلو بايت
Business
What are the best practices for responding to comments and feedback?
In today’s digital world, engaging with your audience through comments and feedback is more...
بواسطة Dacey Rankins 2025-02-20 16:51:59 0 4كيلو بايت
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov