JS Object Def. Property Get doubt

Hi. With a get method, I limit public access to an obj member so one can only read a property. The code below illustrate the situation:

function Bomb() {
  let id = '345drtsTRS*&gkj';
  this.explode = function(msg) {

  Object.defineProperty(this, 'id', {
    get: function() {
      return id;

const b1 = new Bomb();

In the console I do the following test:

b1.id = 'new id'
"new id"

Indeed it’s read only, but wouldn’t JS supposed to throw an error when I tried:

b1.id = 'new id' ???

=== EOF ===

Only when in strict mode. Then it throws a TypeError.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.