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

  • Home
  • SEARCH
  • 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 6231659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:58:25+00:00 2026-05-24T09:58:25+00:00

I’m a bit confused as to the purpose of constructor functions in javascript. I

  • 0

I’m a bit confused as to the purpose of constructor functions in javascript. I know that creating a constructor function through use of the new keyword also creates a new instance of a global object. Is that all one can do with them?

Also, the prototype of the new instance is the same as the global object, so why not just use the global object as many times as you need to instead of creating an instance? Can one add properties to a new instance?

Also, what points about constructor functions am I missing? Any help understanding this would be much appreciated.

  • 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-24T09:58:26+00:00Added an answer on May 24, 2026 at 9:58 am

    “I know that creating a constructor function through use of the new keyword also creates a new instance of a global object.”

    That’s not right. Invoking a function with new create a new object, but it’s not global. It’s an instance of that function, and will have that function referenced as its .constructor.

    The prototype is simply the object that all the instances of the constructor function point to. It lets all your instances reference a common set of properties.

    The prototype is simply the object to which all the instances created by the constructor function contain an implicit pointer. It lets all your instances reference a common set of properties.

    “Can one add properties to a new instance?”

    Yes, they will be added to the object that was created from the constructor and not to its prototype.

    function MyConstructor( arg ) {
        // If you invoke this with "new", 
        //    then "this" will be a reference to the new instance that is returned
    
        this.instance_prop = arg;
    }
    
    MyConstructor.prototype.proto_prop = "baz";
    
    var inst1 = new MyConstructor( "foo" );
    
    var inst2 = new MyConstructor( "bar" );
    
    
    console.log( inst1.instance_prop ); // foo
    console.log( inst1.proto_prop );    // baz
    
    console.log( inst2.instance_prop ); // bar
    console.log( inst2.proto_prop );    // baz
    

    EDIT: Here’s an example that is a very simple DOM wrapper:

    http://jsfiddle.net/CEVwa/

      // constructor
    var DOMWrapper = function(id) {
           // initialize a count property for the instance created
        this.count = 0;
    
           // if a String was passed to the constructor, go head and call fetchById
        if (typeof id === 'string') {
            this.fetchById(id);
        }
    };
       // fetches an element by the ID given, and calls the .setup() method
    DOMWrapper.prototype.fetchById = function(the_id) {
    
           // store the element with this instance
        this.element = document.getElementById(the_id);
        if( this.element != null ) {
            this.setup();
        }
    };
       // stores a function on the .listener property that is passed to
       //      addEventListener. When clicked, the counter is incremented, and 
       //      appended to the element.
    DOMWrapper.prototype.setup = function() {
        if (this.element != null) {
    
               // retain a reference to this instance that the listener can use
            var wrapper = this;
    
                 // store the listener with this instance
            this.listener = function() {
                wrapper.count++;
                wrapper.element.appendChild( 
                    document.createTextNode( ' ' + wrapper.count )
                );
    
                   // when count is 10, call the tear_down method for this instance
                if( wrapper.count === 10 ) {
                    wrapper.tear_down();
                }
            };
            this.element.addEventListener('click', this.listener, false);
        } else {
            this.no_element();
        }
    };
    DOMWrapper.prototype.no_element = function() {
        alert("you must first fetch an element");
    };
        // remove the listener that is stored on the .listener property
    DOMWrapper.prototype.tear_down = function() {
        if (this.element != null) {
            if( typeof this.listener === 'function' ) {
                this.element.removeEventListener('click', this.listener );
            }
        } else {
            this.no_element();
        }
    };
    
    <div id="one">element number one</div>
    <div id="two">element number two</div>
    
    var wrapper1 = new DOMWrapper();  // constructor with no arguments
    wrapper1.fetchById("one");        // you must fetch the element manually
    
    var wrapper2 = new DOMWrapper( "two" ); // with a string, it will fetchById for you
    

    The constructor places a separate count property initialized to 0 on each instance that is created from it.

    If you pass the constructor a String, it will automatically call the fetchById that is on the prototype. Otherwise you can call fetchById() directly after the new instance is returned.

    Upon a successful getElementById, the fetchById stores the element found on the element property of that instance and calls the setup method, which stores click event listener on the .listener property of the element, and adds uses it to add a click event listener to the element.

    The listener just increments the counter for that instance, and appends the new count to the element.

    Once the count reaches 10, the tear_down method is called to remove the listener.

    • 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 use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

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.