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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:35:49+00:00 2026-05-28T17:35:49+00:00

To add a function to an object, i write it inside of it like

  • 0

To add a function to an object, i write it inside of it like this AddX:

function Foo()
{
    this.x = 1;

    this.AddX = function(y)
    {
      this.x += y;
    }

}

But I found out that other way could be to use prototype:

function Foo()
{
    this.x = 1;
}

Foo.prototype.AddX = function(y)
{
    this.x += y;
}

Using:

obj = new Foo;
obj.AddX(5);
alert(obj.x);

Result is same in both cases. Which route is best or is there any difference between the two ? Is there any performance issue also ?

  • 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-28T17:35:50+00:00Added an answer on May 28, 2026 at 5:35 pm

    In your first approach, addX is an instance method. Thus each instance of Foo will have their own copy of addX.

    While in the second approach, addX is a added onto prototype, and each instance of Foo shares the same prorotype.

    Thus the second approach will save more memory. If you have a large number of instances to be created, then you’d better use the second approach.

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

Sidebar

Related Questions

I want a decorator that would add the decorated function to list, like this
I want to convert function object to function. I wrote this code, but it
I'm trying to add a function that will be accessible throughout all parts of
Classes that implement IEnumerable and provide a public void Add(/* args */) function can
Is it possible to add a jQuery function so that a click in a
is there a function that would add a time delay between 2 lines of
I have a div class that looks something like this: <div class=objects> X </div>
I'm trying to write predicate function for use with STL algorithms. I see that
I read that when sending an object to the function/another object, not the actual
I am trying to make a bookmarklet that opens a popup window. Inside this

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.