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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:21:33+00:00 2026-06-01T01:21:33+00:00

This is just a simple question. Either way works. I prefer my first example,

  • 0

This is just a simple question. Either way works. I prefer my first example, but I didn’t know if doing it this way causes more memory to be allocated than the second example since we are calling “new” on the object….

Example 1

var post = function(){
  var self = this;
  self.div = $('<div></div>');
  self.color = function(color){
    this.div.css({background:color});
  }
}

var p = new post();
p.color("#FFF");

Example 2

var post = function(){
  self = this;
  self.div = $('<div></div>');
}

var color = function(p, color){
  p.div.css({background:color});
}

var p = new post();
color(p, "#FFF");

So, in the first example, the color function I believe will get recreated everytime a new post is called. What if I have a 100 new post(); calls. Is is less efficient than Example 2 where the function is only defined one time?

Does that make sense what I’m asking?

  • 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-01T01:21:34+00:00Added an answer on June 1, 2026 at 1:21 am

    Yes, in Example 1 there will be a separate instance of the “color” function for every instance of a “post” object, whereas there will only be one instance of the function in Example 2. Clearly, if you plan to have a large number of “post” object instances then you are using more memory than you need to.

    In JavaScript, the typical (or prototypical!) way of solving this problem using the best parts of your two examples is as follows (note that I am using “Post” with a capital “P”, per convention of constructor functions which are intended for use with the new operator):

    function Post() {
      this.div = $('<div></div>');
    }
    
    Post.prototype.color = function(color) {
      this.div.css({background:color});
    }
    
    var p = new Post();
    p.color("#FFF");
    

    When looking for a property on an object (e.g. “p.color” in our examples), if the property isn’t defined directly on the instance then it is looked up as an attribute of the “prototype” of the function which constructed the object (e.g. “Post.prototype.color”). This also means you can define instance methods on the prototype and override them by assigning new functions on individual instance property names directly, if you want.

    This way we still get the nice object-oriented syntax of calling “p.color(...)” and the benefit of only one function method instance which is shared by all “Post” instances.

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

Sidebar

Related Questions

This might be a simple question but I'm just picking this up so please
This should be a simple question, but I just can't recall the relevant API.
This should be a simple question, but I just can't seem to figure it
There might be a really simple solution to this question, but i just don't
This is just a simple question. I've been reading the source of something which
[see later answer for more] I think this is just a simple rails question,
Simple question. Just wondering if this can be done without me having to enforce
Alright, this is a very simple question. I just installed Tomcat 6 on my
im struggling with syntax here: hopefully this question is v simple, im just miising
Best explained with code I think, this is just a simple example: public class

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.