New keyword, how to use when and where?

So I read and have only used the new keyword with classes and constructor functions.
However I am seeing in the JS Data Structures… that there is no class or constructer function, but only a function with all the method functions located inside of it… create an empty obj with new keyword… I though you could only use new with classes/constructors. See at the bottom where the new is uses.

function mySet() {
    // the var collection will hold the set
    var collection = [];
    // this method will check for the presence of an element and return true or false
    this.has = function(element) {
        return (collection.indexOf(element) !== -1);
    // this method will return all the values in the set
    this.values = function() {
        return collection;
    // this method will add an element to the set
    this.add = function(element) {
            return true;
        return false;
    // this method will remove an element from a set
    this.remove = function(element) {
            index = collection.indexOf(element);
            return true;
        return false;
    // this method will return the size of the collection
    this.size = function() {
        return collection.length;
    // this method will return the union of two sets
    this.union = function(otherSet) {
        var unionSet = new mySet();
        var firstSet = this.values();
        var secondSet = otherSet.values();
        return unionSet;
    // this method will test if the set is a subset of a different set
    this.subset = function(otherSet) {
        var firstSet = this.values();
        return firstSet.every(function(value) {
          return otherSet.has(value);
var setA = new mySet(); 
var setB = new mySet();

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.


1 Like

The constructor function is the same thing as that function. Classes are basically what’s called syntactic sugar; they do the same thing, but in a way that’s clearer.

If you invoke a function with new it creates an object out of thin air. It will assign this properties to the new object


Pretty sure someone will give a better answer but after you create a class object you wont be able to use the methods inside it directly.

you have to create a new instance of the object be using the new keyword that can take its own arguments


class MyObject {
        this.word = word
    say_something(other_word = null){
        return this.word + other_word

var obj = new MyObject('hello')
var other_obj = new MyObject('goodbye')
var another_one = new MyObject('see ya ')

var result_a  = obj.say_something()
var result_b = other_obj.say_something()
var result_c = another_one.say_something('later')

console.log(result_a) // returns hello
console.log(result_b) // returns goodbye
console.log(result_c) // returns see ya later
1 Like