0

I have been learning self-taught JavaScript for a while now and I just started refactoring a canvas game I made, and I thought to check if my way of writing classes was 'correct' and I stumbled upon a new way to write them.

So my question is which of the following is the best way to create objects in JavaScript, and if they are all virtually the same which one is more widely accepted, or when is it appropriate to use each one.

Class declaration:

This is the one that I did not know was available in JS.

class Person {
    constructor(name) {
        this.name = name;
    }
}

let Me = new Person("John");

Object literal:

From what I can tell this is good for one-off objects.

let Me = {
    type: "Person",
    name: "John";
};

Function Object:

I don't know if this is the proper name but I don't know what else to call it.

function Person(name) {
    this.name = name;

    this.sayName = function() {
        return "Hi, my name is " + this.name;
    };
}

let me = new Person("John");

Prototypes:

From what I can tell they are used for adding extra functionality to preexisting objects (i.e. a value mapping function that works on the defualt JS number objects)

function Person(name) {
    this.name = name;
}

Person.prototype.sayName = function() {
    return "Hi, my name is " + this.name;
};

let me = new Person("John");
Zelkins
  • 723
  • 1
  • 5
  • 22
  • 2
    There are a number of questions already that seem to answer this, such as [*Why should I use ES6 classes?*](https://stackoverflow.com/questions/30783217/why-should-i-use-es6-classes/30783368?s=8|0.2479#30783368) and [*are es6 classes just syntactic sugar for the prototypal pattern in javascript?*](https://stackoverflow.com/questions/36419713/are-es6-classes-just-syntactic-sugar-for-the-prototypal-pattern-in-javascript?s=10|0.2341). Your "Function Object" method is called a constructor (all native functions are constructors) and goes with prototypes, they are aspects of the same concept. – RobG Jul 12 '17 at 03:30
  • 1
    BTW, the answer to this is highly subjective (what are your criteria for "best"?) so likely off topic here. – RobG Jul 12 '17 at 03:33
  • This series of youtube videos might help. https://www.youtube.com/playlist?list=PL0zVEGEvSaeHBZFy6Q8731rcwk0Gtuxub – user2875289 Jul 12 '17 at 03:36
  • also this https://medium.com/javascript-scene/the-two-pillars-of-javascript-ee6f3281e7f3. But as @RobG said, this is a highly subjective topic. You might want to google more and read more from others. – user2875289 Jul 12 '17 at 03:38
  • Possible duplicate of [are es6 classes just syntactic sugar for the prototypal pattern in javascript?](https://stackoverflow.com/questions/36419713/are-es6-classes-just-syntactic-sugar-for-the-prototypal-pattern-in-javascript) – Micha Wiedenmann Jul 12 '17 at 09:52

1 Answers1

1

You've already identified the drawback of the object literal: it only creates one object. Often this is what you want, but when you need multiple objects with lots of shared things you'll want to use functions.

The class syntax is essentially syntactic sugar for the constructor+prototype syntax. They do about the same thing, the differences are in class providing simpler syntax and some safeguards but having lower backwards compatibility.

The constructor with the method created inside should generally not be used, see Use of 'prototype' vs. 'this' in JavaScript? for the difference.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375