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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:11:46+00:00 2026-05-31T20:11:46+00:00

I’ ve tried combining the module pattern with a constructor function but it seems

  • 0

I’ ve tried combining the module pattern with a constructor function but it seems I’ m looking over something.
After instantiating two objects and showing their properties both instance var’ s seem to reference the same object.

This is a small example I put together.

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">     
        <title>test</title>

        <script type="text/javascript">
            var guidUtils = {
                /* Generates a simple UUID/GUID based on some randomisation.
                 *
                 */
                guidGenerator:function () {
                    var S4 = function () {
                        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
                    };
                    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
                }
            };


            var Car = (function () {

                // private variables and functions
                var thiz;
                var ID = 'bar';
                var model;

                var setID = function(id){
                    ID = id;
                };

                var getID = function(){
                    return ID;
                };

                var  setModel = function(pModel){
                    model = pModel;
                };

                var  getModel = function(){
                    return model;
                };

                // constructor
                var Car = function (pModel) {
                    thiz=this;
                    ID = guidUtils.guidGenerator();
                    model = pModel;
                    init();
                };

                //intialisation
                function init(){

                }

                // prototype
                Car.prototype = {
                    constructor: Car,
                    getID: getID,
                    getModel: getModel

                };

                // return Car
                return Car;
            })();

            function show() {
                var car1 = new Car("audi");
                var car2 = new Car("BMW");
                var car1div = document.getElementById("car1");
                var car2div = document.getElementById("car2");
                car1div.innerHTML=car1.getID()+" is a "+car1.getModel();
                car2div.innerHTML=car2.getID()+" is a "+car2.getModel();
            }

        </script>       
    </head
    <body>
        <a onclick="show()" href="javascript:void(0);">show</a>
        <div id="car1"></div>
        <div id="car2"></div>
    </body>
</html>

The result is:

screenshot of result

Could some explain where exactly I’ m making the wrong assumptions?

  • 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-05-31T20:11:47+00:00Added an answer on May 31, 2026 at 8:11 pm
     var thiz;
     etc... 
        // constructor
        var Car = function (pModel) {
            thiz=this;
            etc...
    

    is the problem. This creates a kind of static variable, shared by all instances of the class. All instance methods and properties have to be bound to the prototype and this. Eg:

        var Car = function (pModel) {
            this.ID = guidUtils.guidGenerator();
            this.model = pModel;
            this.init();
        };
    
        // Removed: "var setID = " Instead, bind it to the prototype:
        Car.prototype.setId = function(id){
            this.ID = id;
        };
    

    See also:

    • MDN: this as a constructor
    • MDN: new description
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to construct a data frame in an Rcpp function, but when I
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.