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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:11:20+00:00 2026-05-11T00:11:20+00:00

Could you explain the difference between setting methods in the constructor and through prototype

  • 0

Could you explain the difference between setting methods in the constructor and through prototype object? The following code shows these two ways of setting the methods – say_hello and say_bye both work fine:

function MessageClass() {   this.say_bye = function() { alert('see ya'); }; }  MessageClass.prototype.say_hello = function() { alert('hello'); };  x = new MessageClass(); x.say_hello(); x.say_bye(); 
  • 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. 2026-05-11T00:11:21+00:00Added an answer on May 11, 2026 at 12:11 am

    foxxtrot and annakata are both correct, but I’ll throw in my 2 cents.

    If you use the prototype then each instance of the ‘MessageClass’ is really referencing the same functions. The functions exist in memory only once and are used for all instances. If you declare the methods in the constructor (or otherwise add it to a specific instance) rather than the prototype then a new function is created for each instance of MessageClass.

    That being said, there is probably not any noticeable performance difference for most cases and it is unlikely that you will see a memory usage difference either. I would go with the prototype method unless you have a compelling reason to do otherwise. The only reason I can thing that you might want to declare a method in the constructor is if you need a closure. For example, if you have event handlers or you wanted to simulate private properties with getters/setters you might do:

    function MessageClass() {     var self = this;     this.clickHander = function(e) { self.someoneClickedMe = true; };      var _private = 0;     this.getPrivate = function() { return _private; };     this.setPrivate = function(val) { _private = val; }; } 

    EDIT: Because there has been discussion about how this effects objects extended by another object with functions assigned in the constructor I’m adding a bit more detail. I might use the term ‘class’ to simplify the discussion, but it is important to note that js does not support classes (that doesn’t mean we can’t do good OO development) or we would not be discussing this issue.

    Most javascript libraries call the constructor on the base class and the sub class. (e.g. Prototype.js’s Object.extend) This means that methods assigned in the constructor of each will be available on the resulting objects. However, if you are extending objects yourself there can be unexpected consequences.

    If I take the MessageClass above and extend it:

    function ErrorMessageClass() {} ErrorMessageClass.prototype = new MessageClass();  errorMsg = new ErrorMessageClass(); 

    Then errorMsg will have a getPrivate and setPrivate method on it, but they may not behave as you would expect. Because those functions were scoped when they were assigned (i.e. at ‘ErrorMessageClass.prototype = new MessageClass()’ not only are the get/setPrivate methods shared, the _private variable gets shared across all instances of ErrorMessageClass as well. This essentially makes _private a static property for ErrorMessageClass. For example:

    var errorA = new ErrorMessageClass(); var errorB = new ErrorMessageClass(); errorA.setPrivate('A'); console.log(errorA.getPrivate()); // prints 'A' console.log(errorB.getPrivate()); // prints 'A' errorB.setPrivate('B'); console.log(errorA.getPrivate()); // prints 'B' 

    Likewise with the clickHandler function and someoneClickedMe property:

    errorA.clickHandler(); console.log(errorA.someoneClickedMe); // prints 'true' console.log(errorB.someoneClickedMe); // prints 'true' 

    However, change those function definitions to use this._private:

    this.getPrivate = function() { return this._private; }; this.setPrivate = function(val) { this._private = val; }; 

    and behavior of instances of ErrorMessageClass becomes more of what you would expect:

    errorA.setPrivate('A'); errorB.setPrivate('B'); console.log(errorA.getPrivate()); // prints 'A' console.log(errorB.getPrivate()); // prints 'B' 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 89k
  • Answers 89k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can query the absolute screen position with Component.getLocationOnScreen(). Is… May 11, 2026 at 5:58 pm
  • Editorial Team
    Editorial Team added an answer you could add "@" before your command to surpress that… May 11, 2026 at 5:58 pm
  • Editorial Team
    Editorial Team added an answer You'll have to make a PInvoke call to ChangedisplaySetting. Here's… May 11, 2026 at 5:58 pm

Related Questions

I'm a newbie in Database design and in Hibernate too. I started reading the
I have been trying to explain the difference between switch statements and pattern matching(F#)
Possible Duplicate: Could you explain STA and MTA? All ThreadPool threads are in the
Could someone explain the difference between Software Design and Software Architecture? More specifically; if

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.