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 8780117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:57:45+00:00 2026-06-13T19:57:45+00:00

There are lots of ways of doing the same thing in JavaScript. I have

  • 0

There are lots of ways of doing the same thing in JavaScript. I have however picked up some ways, and some ways I frankly don’t understand. Could anyone please help me clarify some things? (I first learnt OOP in PHP.)

So a class can be made like this:

var object = new class(constructparams) {
    var private_members; // Can be accessed from within the code inside this definition only.
    this.public_members; // Can be accessed from everywhere.

    var private_functions = function() {}
    this.public_functions = function() {}
}

object.prototype.semi_public_members = function() {
    // Will be public, but can only access public members and methods.
    // E. g. private_members; is not available here.
}

Is this all correct so far?

Then someone likes the self-executing anonymous function approach to create a namespace. What is the point of that, when you have this way above that does the same thing, provides a namespace?

And lastly you have the object literal notation that I don’t understand.

var object = { // Something strange in here }

What is going on in there? Is it JSON? How is it used, how can I use it. What are the benefits of using this way instead of using the method I described? Why would you prototype instead of making the class correctly the first time?

  • 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-13T19:57:47+00:00Added an answer on June 13, 2026 at 7:57 pm

    Explaining the behaviour of different things in a constructed object by example:

    // Defined as a variable from an anonymous function
    // so that there is scope closure over variables
    // shared across all instances and the prototype.
    // If this isn't important, you don't need to close
    // scope around it, so define directly
    var ConstructedObject = (function constructorCreator () {
        // Define any variables/methods to be shared across
        // all instances but not polluting the namespace
        var sharedVariable = 'foo';
    
        // Next the actual constructor
        function ConstructedObject () {
            // Variables here are normally used to help
            // each instance and will be kept in memory as
            // long as the instance exists
            var instanceVariable = 'bar';
            // instance-specific properties get defined
            // using the "this" keyword, these are the
            // properties expected to be changed across
            // each different instance
            this.instanceProperty = true;
            this.instanceMethod = function () { return instanceVariable; };
            this.changeInstanceVar = function () { instanceVariable = 'foo'; };
                // you do have access to the shared
                // variables here if you need them.
        }
        // After the constructor, you set up the
        // prototype, if any. This is an object of shared
        // properties and methods to be inherited by every
        // instance made by the constructor, and it also
        // inherits the prototype's prototype, too.
        // Lets use a literal object for simplicity.
        ConstructedObject.prototype = {
            // Accessing the instance to which a method
            // applies is done using the "this" keyword,
            // similar to in the constructor
            sharedMethod : function () { return [sharedVariable, this.instanceMethod(),this.instanceProperty]; },
            changeSharedVar : function () { sharedVariable = 'bar'; }
            // properties may also be defined
        };
        // Finally, the constructor is returned so it
        // can be kept alive outside of the anonymous
        // function used to create it
        return ConstructedObject;
    // and the anonymous function is called to execute
    // what we've done so far
    })();
    

    After executing the above code, you have a constructor that creates objects with both instance-specific and shared variables. Now let’s look at how they behave by creating two of instances and comparing them before and after some changes.

    // First create the two instances
    var myObjA = new ConstructedObject(),
        myObjB = new ConstructedObject();
    // Now compare them, the sharedMethod method we
    // used in the prototype offers an easy way to
    // do this
    console.log( myObjA.sharedMethod(), myObjB.sharedMethod() );
    // ["foo", "bar", true] ["foo", "bar", true]
    // Next lets change the different variables in
    // myObjB so we can see what happens, again the
    // change* methods defined before let us do this
    // easily
    myObjB.changeInstanceVar();
    myObjB.changeSharedVar();
    // For completeness, lets also change the property
    // on myObjB.
    myObjB.instanceProperty = false;
    // Now when we compare them again, we see that our
    // changes to the myObjB instance have only changed
    // the shared variables of myObjA
    console.log( myObjA.sharedMethod(), myObjB.sharedMethod() );
    // ["bar", "bar", true] ["bar", "foo", false]
    

    Here are the two logged statements together for easier viewing

    //     myObjA               myObjB
    ["foo", "bar", true] ["foo", "bar", true]
    ["bar", "bar", true] ["bar", "foo", false]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are lots of ways for browser detecting in JavaScript. As far as i
This is a simplification of the issue (there are lots of ways of doing
There are lots of java apps on my simulator menu screen which I have
I have seen lots of ways of running Perl code or scripts, with different
Quick question... there are lots of ways to create an image map (old school,
There are lots of ways to create a login form for web apps and
I know there's lots of ways to do the click event handling for google
There are lots of different ways to save state in html5, how do you
I've seen lots of ways to backup a single repository in subversion. Is there
There's lots of questions on SO related to this, but the ones I scanned

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.