What is the difference between defining an object constructor using the “class” keyword and using the “function” keyword?
09:07 14 Nov 2018

Usually, I create a class and then instantiate objects from that class, however, as I was watching a tutorial I saw the guy creating objects just by using a function without even having a class.

In the code below I have used both ways just to check what the difference would be when I console log the arrays that contain the objects. I fill the first array with objects generated using my class and the second array with the objects generated by the function.

After I console.log() both arrays, they seem to be identical. So what is the point of creating a class if I can create objects without having it?

var w = 40;
var cols, rows;
var grid = [];
var grid1 = [];

class Cell {
    constructor(i, j) {
        this.i = i,
        this.j = j
    }
}

function Cell2(i, j) {
    this.i = i;
    this.j = j;
}

function setup() {
    createCanvas(400, 400);
    cols = floor(width/w);
    rows = floor(height/w);

    for (var i = 0; i < cols; i++) {
        for (var j = 0; j < rows; j++) {
            var cell = new Cell(i,j);
            grid.push(cell);
        }
    }

    for (var i = 0; i < cols; i++) {
        for (var j = 0; j < rows; j++) {
            var cell1 = new Cell2(i,j);
            grid1.push(cell1);
        }
    }
}
javascript constructor es6-class