DEV Community

francesco agati
francesco agati

Posted on

1

Creating Objects in JavaScript: Closures, Prototypes, and ES6 Classes

In JavaScript, there are several ways to create objects. Each method has its own advantages and use cases. We will explore three common methods: closures, prototypes, and ES6 classes with examples.

1. Using Closures

A closure is a function that remembers the environment in which it was created. This allows us to encapsulate data within functions.

function createPerson(name) {
    let age = 0;

    return {
        getAge: function() {
            return age;
        },
        growUp: function() {
            age++;
        }
    };
}

const person1 = createPerson("Alice");
console.log(person1.getAge()); // Output: 0
person1.growUp();
console.log(person1.getAge()); // Output: 1
Enter fullscreen mode Exit fullscreen mode

2. Using Prototypes

Prototypes allow us to create objects with shared properties and methods.

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

Person.prototype.getAge = function() {
    return this.age || 0;
};

Person.prototype.growUp = function() {
    if (!this.age) {
        this.age = 1;
    } else {
        this.age++;
    }
};

const person2 = new Person("Bob");
console.log(person2.getAge()); // Output: 0
person2.growUp();
console.log(person2.getAge()); // Output: 1
Enter fullscreen mode Exit fullscreen mode

3. Using ES6 Classes

ES6 classes provide a more traditional class-based syntax, making it easier to understand and use.

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

    getAge() {
        return this.age;
    }

    growUp() {
        this.age++;
    }
}

const person3 = new Person("Charlie");
console.log(person3.getAge()); // Output: 0
person3.growUp();
console.log(person3.getAge()); // Output: 1
Enter fullscreen mode Exit fullscreen mode

We explored three methods to create objects in JavaScript: closures, prototypes, and ES6 classes. Each method has its own strengths and use cases.

  • Closures are useful for encapsulating data within functions.
  • Prototypes allow us to share properties and methods among multiple objects.
  • ES6 Classes provide a more traditional class-based syntax, making it easier to understand and use.

Neon image

⚑ Set up a Neon project in seconds and connect from a Next.js application

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started β†’

Top comments (3)

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ β€’

A closure is a function that remembers the environment in which it was created

This is not correct. If it were correct there would be no point in having different words for 'closure' and 'function' since ALL functions remember the environment in which they were created. Every function has an associated closure, but a closure is not a function.

Collapse
 
francescoagati profile image
francesco agati β€’

yes but we speak of javascript and is only for simplify the article

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ β€’

I am speaking about JS. Your definition does not differentiate a closure from a normal function. This is wrong as they are two different things.

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started β†’

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere β€œthank you” often brightens someone’s dayβ€”share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay