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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:19:17+00:00 2026-06-09T18:19:17+00:00

This would presumably be the safest way ( Case A ): var myClass =

  • 0

This would presumably be the safest way (Case A):

var myClass = function() { };
myClass.prototype = {
    doSomething : function() { alert('Something'); }
};

This is the alternative (Case B):

var myClass = function() {
    this.doSomething = function() { alert('Something'); };
};

I’m under the impression that by doing this as shown in Case B, doSomething would be a member, and the function would be defined once for each myClass object I instantiate so that it will exist 100 times in memory for 100 instances, whereas in Case A the function will only exist in one place in memory and different instances will merely reference the prototype.

Am I understanding this correctly?

As a bonus question: When doing it as in Case B, chrome developer gives me intellisense for doSomething, but I must expand __proto__ for an instance to see it. How come it doesn’t show up on the object itself? That is, why doesn’t prototype members show on the object, but get stuck back down on __proto__? I would have preferred if the __proto__ stack would get flattened and show up on the object directly. Is there another Case that will allow this to happen?

  • 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-09T18:19:19+00:00Added an answer on June 9, 2026 at 6:19 pm

    Firstly, in case B, you are merely creating a global function, not attaching it to the instance. You meant:

    this.doSomething = function() { }
    

    Secondly, the first will be faster. Though I can’t find the link now, jQuery honcho John Resig did a detailed blog post on this showing speed tests on prototypal inheritance of methods vs. methods declared on the instance. Inheritance was notably faster.

    In terms of ethos, I’ve always much favoured inheritance. This is the place for reusable, cross-instance functionality. Adding it to each instance has the sole benefit of allowing you to declare methods inside a single, convenient closure, in your constructor, but that’s it.

    If this is your reason for liking pattern B, it’s possible to do this whilst still a) having the methods inherited; b) not redeclaring them at every instantiation.

    function SomeClass() {
        if (!SomeClass.prototype.someMethod) {
            SomeClass.prototype.someMethod = function() {}
        }
    }
    

    This will slightly slow down the initial instantiation, though, as it is responsible for setting up the prototype – not really the job of an instantiation process.

    There is also a programmatical difference to be aware of between your two cases:

     function SomeClass(name) {}
     SomeClass.prototype.someMethod = function() {};
     var instance = new SomeClass();
     console.log(!!instance.someMethod); //true
     console.log(instance.hasOwnProperty('someMethod')); //false
    

    The last line is false because the method is inherited, not owned by the instance. With your pattern B, this will resolve to true.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This would be it : y'(t)=y(t)/t-t^2/y^2*t y(1)=1 i have tried: function hazi3b() [T,Y] =
This would seem to be the case in Firefox 3.5+, there I can instantiate
This would be easy to do with regular array with a simple for statement.
This would be an example of database table: PHP script should limit query result
This would be an Array of data: $array = array(sad,bow,blabla); IF $input = I
This would be a HTML file: <li class=msgln>hello</li><li class=msgln>hi</li><li class=msgln>hey</li> And php script: $fp
I would have thought that this would be an easy thing to do, but
I thought this would be fairly simple but it turns out not to work
I assumed that this would provide me with the list of follower names instead
I was wonder if this would be possible and how to do it. I

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.