ES6 const for prototyping objects in JavaScript; is this a sample?

I often see what is constused to create the base object.

  • Is this a template?
  • If so, what are the benefits?

An example from here :

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

const waffle = Object.create(food);
waffle.init('waffle');
console.log(waffle.type);
+4
source share
1 answer

constannounces a read link. You cannot change the value if declared with const. See Documentation from MDN :

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

For instance:

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

So, it constprovides the following data:

  • . ( )
  • .
  • .

!

, : var let. . , var . , var, let. .

ES6 ( MDN):

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

Babel ( 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;
};
+5

All Articles