Question: I want to create Class with writable:false-fields.
The task is:

* I need to use constructor for these values;
* We can write these values only once.
F.e:

```
 Class Room {
   constructor(length, width) {
    this.length = length; 
    this.width = width; 
   }
}
let room = new Room(20, 10); 
console.log(room.length) // 20
room.length = 10000 // Error ```

I have no idea how to do it. Does defineProperty method fit?

```

Answer: 
Calling `Object.defineProperty` in the constructor works:

```js
class Room {
  constructor(length, width) {
    Object.defineProperty(this, 'length', { value: length });
    Object.defineProperty(this, 'width', { value: width });
  }
}
let room = new Room(20, 10);
console.log(room.length) // 20
room.length = 10000 // Does not do anything, throws in strict mode
console.log(room.length) // 20
```

If you want to throw an error when the assignment is attempted, either run the script in strict mode, or use getters/setters instead:

```js
class Room {
  constructor(length, width) {
    Object.defineProperty(this, 'length', { get() { return length }, set() { throw new Error() }});
    Object.defineProperty(this, 'width', { get() { return width }, set(){ throw new Error() }});
  }
}
let room = new Room(20, 10);
console.log(room.length) // 20
room.length = 10000 // setter throws
```
If this should be the behaviour for all properties of the instance, and are looking for an immutable object, then you can call `Object.freeze`:

```js
class Room {
  constructor(length, width) {
    this.length = length;
    this.width = width;
    Object.freeze(this);
  }
}

let room = new Room(20, 10);
console.log(room.length); // 20;
room.length = 10000; // No effect. Throws error in strict mode
console.log(room.length); // 20
room.newProp = "hi"; // No effect either.
console.log("newProp" in room); // false
```