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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:29:04+00:00 2026-05-22T22:29:04+00:00

Possible Duplicate: Use of 'prototype' vs. 'this' in Javascript? I went to various websites

  • 0

Possible Duplicate:
Use of 'prototype' vs. 'this' in Javascript?

I went to various websites but not able to understand the difference between the following ways of adding methods to custom objects:

Method 1:

function circle(radius){
     this.radius = radius;
     this.area = function(){ return 3.14*this.radius*this.radius;}
}

Method 2:

function circle(radius){
     this.radius = radius;
}
circle.prototype.area = function(){ return 3.14*this.radius*this.radius; }

Are there any performance or design issues that one of the methods has and the other does not?

  • 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-22T22:29:05+00:00Added an answer on May 22, 2026 at 10:29 pm

    Here is one way to see the difference:

    var circle1 = circle(1);
    var circle2 = circle(1);
    alert(circle1.area == circle2.area);
    

    For Method1 you will see false while Method2 yields in true. That’s because in the first case you assign a new closure function to each object created, two objects end up with different area functions even though both do the same thing. In the second case the objects share the same prototype object with its area method, that method is of course identical in both cases.

    Usually Method2 is preferable for several reasons:

    1. Object creation is faster because only custom properties need to be initialized.
    2. You don’t waste memory on instantiating multiple copies of the same thing.
    3. You can easily find out which of the object properties have custom values by calling Object.hasOwnProperty.

    There are some disadvantages as well to keep in mind.

    1. You always spend time on setting up the prototype even if you never create any objects.
    2. Property access is slightly slower because the JavaScript engine needs to check object properties first and then the properties of the prototype object (modern JavaScript engines optimize this pretty well however).
    3. There is a common fall trap of putting objects or arrays on the prototype, these will be shared between all object instances.

    Here is an example of the common mistake:

    function NumberCollection()
    {
    }
    NumberCollection.prototype = {
        numbers: [],
        sum: function()
        {
            var result = 0;
            for (var i = 0; i < this.numbers.length; i++)
                result += this.numbers[i];
            return result;
        }
    }
    
    var c1 = new NumberCollection();
    c1.numbers.push(5);
    alert(c1.sum());   // Shows 5
    var c2 = new NumberCollection();
    c2.numbers.push(6);
    alert(c2.sum());   // Oops, shows 11 because c1.numbers and c2.numbers is the same
    

    The correct approach here would be:

    function NumberCollection()
    {
        this.numbers = [];
    }
    NumberCollection.prototype = {
        numbers: null,
        sum: function()
        {
            var result = 0;
            for (var i = 0; i < this.numbers.length; i++)
                result += this.numbers[i];
            return result;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: JavaScript: Class.method vs. Class.prototype.method What's the difference between creating a prototype like
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: Escape string for use in Javascript regex I have a msg like
Possible Duplicate: Use of var keyword in C# I don't really understand why you
Possible Duplicate: When do you use the “this” keyword? If I have the following
Possible Duplicate: Use SVN Revision to label build in CCNET I'm working through the
Possible Duplicate: When to use “strictfp” keyword in java? What is the use of
Possible Duplicate: Internationalization in SSRS We use SQL Server Reporting Services for our web
Possible Duplicate: How can I use a carriage return in a HTML tooltip? I'd
Possible Duplicate: When should you use 'friend' in C++? I was brushing up on

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.