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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:52:26+00:00 2026-05-13T06:52:26+00:00

What are the technical reasons for using .prototype instead of declaring functions and members

  • 0

What are the technical reasons for using .prototype instead of declaring functions and members inside the object itself. It is easiest to explain with code examples.

What are the advantages of using:

RobsObject = function(data){
    this.instanceID = data.instanceID;
    this._formButton = document.getElementById('formSubmit_' + this.instanceID);
    if(this._formButton)
    {
        //set a click listener that
        //points to this._onSubmit, this._onSuccess, and this.onFailure
    }
};

RobsObject.prototype = {
    _onSubmit: function(type, args)
    {
        //make an ajax call
    },

    _onSuccess: function(type, args)
    {
        //display data on the page
    },

    _onFailure: function(type, args)
    {
        //show an alert of some kind
    },
};

As oppose to declaring your functions inside of the Object like:

RobsObject = function(data){
    this.instanceID = data.instanceID;
    this._formButton = document.getElementById('formSubmit_' + this.instanceID);
    if(this._formButton)
    {
        //set a click listener that
        //points to this._onSubmit, this._onSuccess, and this.onFailure
    }

    this._onSubmit = function(type, args)
    {
        //make an ajax call
    }

    this._onSuccess = function(type, args)
    {
        //display data on the page
    }

    this._onFailure = function(type, args)
    {
        //show an alert of some kind
    }
};

Thanks.

Edit: As many of you have pointed out my functions in the second code snippet should have ‘this’ in front of them in order to be public. So I added it. Just a mistake on my part.

  • 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-13T06:52:27+00:00Added an answer on May 13, 2026 at 6:52 am

    Everything declared in a constructor function’s prototype is shared by all instances of that constructor function. If you define functions in the constructor function, then each instance gets its own copy of the function, which wastes memory (and could potentially cause problems if you compare properties between two instances later).

    Also, in your example the functions declared in the constructor function are private to the scope of the function. They cannot be called as member methods on instances. For that you would need to assign them to properties of the object:

    MyObject = functon() {
      // ...
    
      this.myMethod = function() {
        // ...
      };
    }
    

    Douglas Crockford has a good write-up of prototypical inheritance that is definitely worth checking out: Prototypical Inheritance in JavaScript.

    UPDATE: Brief Prototype Summary

    When you create a new object using a constructor function, the value of the function’s prototype property is assigned as the new object’s prototype object. (Yes, the names are confusing!) This is a lot like assigning a superclass in a class-based language (but not quite! Read Crockford’s page!)

    // MyObject constructor function:
    MyObject = function() {
      this.a = 1;
    }
    
    // Define an object to use as a prototype.
    var thePrototype = { b: 2 };
    
    // Assign thePrototype as the prototype object for new instances of MyObject.
    MyObject.prototype = thePrototype;
    
    // Create an instance of MyObject.
    var x = new MyObject();
    // Everything in thePrototype is available to x.
    console.log(x.b);
    
    // x's prototype is a reference to thePrototype, so updating it affects x.
    thePrototype.c = 3;
    console.log(x.c);
    
    // Setting properties on x always sets them *on x*, even if the property is
    //  defined on the prototype:
    x.b = 0;
    y = new MyObject();
    console.log(x.b);
    console.log(y.b);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For technical reasons, I can't use ClickOnce to auto-update my .NET application and its
I know it is customary, but why? Are there real technical reasons why any
Is there any (technical) reason that C# requires all generic type parameters to be
My technical lead insists on this exception mechanism: try { DoSth(); } catch (OurException)
Not very technical, but... I have to implement a bad words filter in a
What is the technical difference between a process and a thread? I get the
This is not a technical problem, but very annoying. Does anyone know how to
This is not a technical question, but given that there are a few iPhone
I am looking for a more technical explanation than the OS calls the function.
I've got a number of non-technical users that all share a set of project

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.