[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Functions/set [Back]  [Original]

- JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.

set .

In this article

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

language.current = "EN";
language.current = "FA";

console.log(language.log);
// Expected output: Array ["EN", "FA"]

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

prop

.

val

prop .

expression

ECMAScript 2015 , .

JavaScript (setter) . (getter) ' ' . , .

set .

  • .

  • .

  • , .

    js
    {
      set x() { }, set x() { }
    }
    
  • , .

    js
    {
      x: ..., set x() { }
    }
    

language current . current , log .

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

current , undefined .

delete

delete .

js
delete language.current;

defineProperty

Object.defineProperty() .

js
const o = { a: 0 };

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

o.b = 10;
//  , a  10 / 2 = 5 

console.log(o.a);
// 5

js
const expr = "foo";

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

console.log(obj.baz);
//  "bar"

obj.foo = "baz";
//  run the setter

console.log(obj.baz);
//  "baz"

Specification
ECMAScript 2027 LanguageSpecification
# sec-method-definitions

See also


Web Proxy Viewer  |  New URL  |  Original Page