setter

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

set 語法會在物件屬性被嘗試定義時,將其屬性綁定到要呼叫的函式內。

嘗試一下

語法

{set prop(val) { . . . }}
{set [expression](val) { . . . }}

參數

prop

要綁定到給定函式的屬性名。

val

變數別名,該變數擁有要被嘗試安插到 prop 的數值。

expression

從 ECMAScript 2015 開始,可以使用計算屬性名(computed property name)表達式,綁定到給定函式。

描述

JavaScript 的 setter 能在嘗試修改指定屬性時,執行給定函式。Setter 最常用於和 getter 一同建立虛擬屬性(pseudo-property)。你不可能給同一個屬性賦予 setter 與實際值。

使用 set 語法時,請注意以下情況:

delete 操作符可移除 setter。

示例

在物件初始器的新物件定義 setter

這裡會給物件 language 定義稱為 current 的虛擬屬性。在指派數值時 log 會和該值一同更新:

js
var 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']

請注意 current is not defined and any attempts to access it will result in undefined.

使用 delete 操作符移除 setter

若想移除 setter 的話,可以直接使用 delete

js
delete o.current;

針對已存在屬性的 setter 使用 defineProperty

To append a setter to an existing object later at any time, use Object.defineProperty().

js
var o = { a: 0 };

Object.defineProperty(o, "b", {
  set: function (x) {
    this.a = x / 2;
  },
});

o.b = 10; // Runs the setter, which assigns 10 / 2 (5) to the 'a' property
console.log(o.a); // 5

使用計算屬性名

js
var expr = "foo";

var obj = {
  baz: "bar",
  set [expr](v) {
    this.baz = v;
  },
};

console.log(obj.baz); // "bar"
obj.foo = "baz"; // 跑 setter
console.log(obj.baz); // "baz"

規範

Specification
ECMAScript Language Specification
# sec-method-definitions

瀏覽器相容性

BCD tables only load in the browser

參見