4

I often see const being used to create a base object.

  1. Is this a pattern?
  2. If yes, what are the advantages?

Example from here:

const food = {
    init: function(type) {
        this.type = type;
    }
}

const waffle = Object.create(food);
waffle.init('waffle');
console.log(waffle.type);
sorenymous
  • 92
  • 7
  • #1 Depends on your definition of "pattern". #2 Declaring variables with *const* just means the value can't be reassigned. No particular non–obvious advantages other than makes compiling from other languages into ECMAScript easier. – RobG May 07 '16 at 12:52

1 Answers1

5

const declares a readonly reference. You cannot change the value if declared with const. See documantation from MDN:

The const declaration creates a read-only reference to a value. It does not mean the value it holds is immutable, just that the variable identifier cannot be reassigned.

For example:

const x = {a:2};//declaration. 
x={b:3};//This statement throws an exception
x.c=4;//This is ok!

So, const provides these:

  1. The initial defined object cannot be reassigned.(readonly)
  2. The properties of the initial object can be reassigned.
  3. New properties can be added to the initial object.

These are perfectly suitable for class definitions!

On the other hand, there are two alternatives to declare a value: var and let. Both cannot provide read-only referencing. Also var doesn't provide a block-scope declaration. Of course you can use either var or let. This is your choice and your usage.

Also in ES6, you can define classes like that (resource from MDN):

class Polygon {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}

And Babel translates it like this (by using var):

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Polygon = function Polygon(height, width) {
  _classCallCheck(this, Polygon);

  this.height = height;
  this.width = width;
};
sorenymous
  • 92
  • 7
ykaragol
  • 6,139
  • 3
  • 29
  • 56
  • 1
    To be a bit more precise: `const` generates an immutable *binding*, i.e. you cannot reassign a different value. The *value* of the binding is still mutable and can be changed in any way possible. – Felix Kling May 07 '16 at 16:12
  • Btw, where did the first example come from? http://stackoverflow.com/questions/34983693/javascript-es6-const-a-is-mutable-why uses the same one... – Felix Kling May 07 '16 at 16:15
  • That example is totally my interpretation of [MDN link provided by me](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/const) – ykaragol May 07 '16 at 16:22
  • Ha, it's too early in the morning for me, I can't even compare code properly :P – Felix Kling May 07 '16 at 16:24
  • 1
    @ykaragol thanks for your elaborate answer. I read MDN doc before I post my question. It is very clear how `const` works. My question was about its use in that specific setting. – sorenymous May 08 '16 at 06:06