[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Errors/Getter_only [Back]  [Original]

TypeError: setting getter-only property "x"( "x") - 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

TypeError: setting getter-only property "x"( "x")

TypeError:           (Edge)
TypeError:      "x" (Firefox)
TypeError:     "prop"  #<Object>,     (Chrome)

In this article

TypeError .

?

, . . non-strict , TypeError strict mode.

, getter . , TypeError 30. . Object.defineProperty() .

js
"use strict";

function Archiver() {
  var temperature = null;
  Object.defineProperty(this, "temperature", {
    get: function () {
      console.log("get!");
      return temperature;
    },
  });
}

var arc = new Archiver();
arc.temperature; // 'get!'

arc.temperature = 30;
// TypeError: setting getter-only property "temperature"

, 16, temperature, , , :

js
"use strict";

function Archiver() {
  var temperature = null;
  var archive = [];

  Object.defineProperty(this, "temperature", {
    get: function () {
      console.log("get!");
      return temperature;
    },
    set: function (value) {
      temperature = value;
      archive.push({ val: temperature });
    },
  });

  this.getArchive = function () {
    return archive;
  };
}

var arc = new Archiver();
arc.temperature; // 'get!'
arc.temperature = 11;
arc.temperature = 13;
arc.getArchive(); // [{ val: 11 }, { val: 13 }]


Web Proxy Viewer  |  New URL  |  Original Page