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 …


This content originally appeared on DEV Community and was authored by francesco agati

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

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

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

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.


This content originally appeared on DEV Community and was authored by francesco agati


Print Share Comment Cite Upload Translate Updates
APA

francesco agati | Sciencx (2025-01-02T20:09:03+00:00) Creating Objects in JavaScript: Closures, Prototypes, and ES6 Classes. Retrieved from https://www.scien.cx/2025/01/02/creating-objects-in-javascript-closures-prototypes-and-es6-classes/

MLA
" » Creating Objects in JavaScript: Closures, Prototypes, and ES6 Classes." francesco agati | Sciencx - Thursday January 2, 2025, https://www.scien.cx/2025/01/02/creating-objects-in-javascript-closures-prototypes-and-es6-classes/
HARVARD
francesco agati | Sciencx Thursday January 2, 2025 » Creating Objects in JavaScript: Closures, Prototypes, and ES6 Classes., viewed ,<https://www.scien.cx/2025/01/02/creating-objects-in-javascript-closures-prototypes-and-es6-classes/>
VANCOUVER
francesco agati | Sciencx - » Creating Objects in JavaScript: Closures, Prototypes, and ES6 Classes. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/02/creating-objects-in-javascript-closures-prototypes-and-es6-classes/
CHICAGO
" » Creating Objects in JavaScript: Closures, Prototypes, and ES6 Classes." francesco agati | Sciencx - Accessed . https://www.scien.cx/2025/01/02/creating-objects-in-javascript-closures-prototypes-and-es6-classes/
IEEE
" » Creating Objects in JavaScript: Closures, Prototypes, and ES6 Classes." francesco agati | Sciencx [Online]. Available: https://www.scien.cx/2025/01/02/creating-objects-in-javascript-closures-prototypes-and-es6-classes/. [Accessed: ]
rf:citation
» Creating Objects in JavaScript: Closures, Prototypes, and ES6 Classes | francesco agati | Sciencx | https://www.scien.cx/2025/01/02/creating-objects-in-javascript-closures-prototypes-and-es6-classes/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.