2020-06-08
1107
#vanilla javascript
Gbolahan Olagunju
19958
Jun 8, 2020 â‹… 3 min read

How to decide between classes v. closures in JavaScript

Gbolahan Olagunju Let's have a chat about your project. Find me on Twitter @iamgbols.

Recent posts:

How To Create Heatmaps In Javascript: Exploring The Heat Js Library

How to create heatmaps in JavaScript: The Heat.js library

This tutorial will explore the application of heatmaps in JavaScript projects, focusing on how to use the Heat.js library to generate them.

Oghenetega Denedo
May 8, 2024 â‹… 7 min read
Eleventy Adoption Guide: Overview, Examples, And Alternatives

Eleventy adoption guide: Overview, examples, and alternatives

Eleventy (11ty) is a compelling solution for developers seeking a straightforward, performance-oriented approach to static site generation.

Nelson Michael
May 7, 2024 â‹… 8 min read
6 CSS Tools For More Efficient And Flexible CSS Handling

6 CSS tools for more efficient and flexible CSS handling

Explore some CSS tools that offer the perfect blend of efficiency and flexibility when handling CSS, such as styled-components and Emotion.

Fimber Elemuwa
May 7, 2024 â‹… 7 min read
Leveraging React Server Components In Redwoodjs

Leveraging React Server Components in RedwoodJS

RedwoodJS announced support for server-side rendering and RSCs in its Bighorn release. Explore this feature for when it’s production-ready.

Stephan Miller
May 6, 2024 â‹… 9 min read
View all posts

3 Replies to "How to decide between classes v. closures in JavaScript"

  1. Why aren’t you building your Closure like your class and getting the best of both worlds?

    let UserClosure = function(firstName, lastName, age, occupation) {
    this.firstName = params.firstName;
    this.lastName = params.lastName;
    this.age = age;
    this.occupation = occupation;

    let privateValue = “Can’t see this!”;
    function privateFunction(args) { // private method }
    }
    UserClosure.prototype.getAge = function() { return this.age; }
    UserClosure.prototype.describeSelf = function() { …. };

    let someOne = new UserClose(“first”, “last”, 55, “dev”);

    This isn’t intended as argumentative. I’m looking for why I should start using classes instead of the above construction in some upcoming work.

  2. Here’s how to get the best of both worlds.

    const Foo = (function() {
    //create a prototype.
    const prot = {
    bar(bas) {
    bas = bas || this.fallbackBas;
    console.log(“bar says ” + bas);
    }
    } //end of prot.

    //constructor.
    return function(fallback) {
    const o = Object.create(prot);
    //new object, prot as prototype.
    o.fallbackBas = fallback;
    return o;
    } //constructor
    })(); //iif

    const f = new Foo(“This is a fallback.”);
    f.bar(“This is not a fallback.”);
    f.bar();

    /*Output:

    bar says This is not a fallback.
    bar says This is a fallback.
    */

    All the funcs are created only once, and other vars can go in the same outer func.

Leave a Reply