Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8011221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:57:56+00:00 2026-06-04T18:57:56+00:00

I’m trying to figure out how to construct my Javascript classes (or singleton objects)

  • 0

I’m trying to figure out how to construct my Javascript classes (or singleton objects) correctly.

var obj = new Object();
obj.foo = 'bar';
obj.method = function() { ...}

var obj = {
    foo : 'bar',
    method : function() { ...}
}

var obj = function(){}
obj.prototype = {
    foo : 'bar',
    method: function() { ... }
}

I want to be able to set a couple of properties and assign the methods available. I would also like to be able to use things like mixins on the objects so I can extend these objects with things like events.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:57:57+00:00Added an answer on June 4, 2026 at 6:57 pm

    I’m trying to figure out how to construct my Javascript classes (or singleton objects) correctly.

    There’s a big difference between those (“classes” and singleton objects). Your first couple of examples are one-off objects (singletons). Your third (last) example creates a constructor function that will allow you to create multiple objects sharing the same prototype. I would recommend augmenting the prototype property on the constructor function rather than replacing it as you’re doing, e.g.:

    var Thingy = function() {   // Or use a function declaration rather than expression
        // Optional initialization code here
    };
    Thingy.prototype.foo = 'bar';
    Thingy.prototype.method = function() {
        // Method code here
    };
    

    (Constructor functions, by convention, start with an upper case letter.)

    Which you use (singleton or constructor function) depends on what you need.

    As of ES2015 (aka “ES6”), it’s simpler, although there’s no new syntax for defining a non-method prototype property (your foo); there probably will be in ES2017 or ES2018, once this proposal moves forward, but until then:

    class Thingy {
        constructor() {
            // Optional initialization code here
        }
        method() {
        // Method code here
        }
    }
    Thingy.prototype.foo = 'bar';
    

    If you need to get into inheritance hierarchies, in the old ES5 syntax there’s a fair bit of plumbing involved:

    var Base = function() {
    };
    Base.prototype.method = function(arg1) {
        // ...
    };
    
    var Derived = function() {
        Base.call(this);
    };
    Derived.prototype = Object.create(Base.prototype);
    Derived.prototype.constructor = Derived;
    Derived.prototype.method = function(arg1) {
        // Using Base's `method`, if desired, is a bit ugly:
        Base.prototype.method.call(this, arg1);
    };
    

    …which is why you used to see libraries stepping in, like Prototype’s Class stuff, or my own Lineage; those are outdated by ES2015 syntax, though, whcih makes it downright easy:

    class Base {
        method(arg1) {
            // ...
        }
    }
    class Derived extends Base {
        method(arg1) {
            // Use's the superclass's `method`, if desired, is easy:
            super.method(arg1);
        }
    }
    

    Re the title of your question:

    What is the correct way to create a Javascript class?

    There are several equally-correct ways to create “classes” of objects in JavaScript, because JavaScript is a very flexible language. There are standard constructor functions, “builder” functions, Object.create (on ES5-enabled environments) which lets you do more direct prototypical inheritance, and several others. One of the great things about JavaScript is that you get to choose your style of “class”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to render a haml file in a javascript response like so:
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.