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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:13:50+00:00 2026-06-10T04:13:50+00:00

I’m struggling to learn object oriented javascript. I have an object constructor that looks

  • 0

I’m struggling to learn object oriented javascript. I have an object constructor that looks like this:

//generic object
function myObject(value1, value2, value3){
this.value1 = value1;
this.value2 = value2;
this.value3 = value3;
this.method1 =method1;
}

function method1(){
alert('hello');
}

function method2(){
alert('hello again');
}

I’m creating new instances of this object like this:

instance1 = new myObject(
1, //value1
2, //value2
3  //value3
)

My problem is this: On a few instances of myObject I need custom values and methods. I would like to add them at the point of creation. But I can’t figure out how to do it without writing the instance name again, which I’m trying to avoid since it would significantly slow down the process of adding new objects.

Is there some way to replace this code..

 instance1 = new myObject(
 1, //value1
 2, //value2
 3  //value3
 )
 instance1.method2 =method2;
 instance1.value4 =4;

..with something that doesn’t require repeating the name “instance1”?

  • 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-10T04:13:52+00:00Added an answer on June 10, 2026 at 4:13 am

    Since you have jQuery available, you can use jQuery.extend() and avoid having to retype instance1. jQuery.extend() copies properties from one object to another.

    jQuery.extend(instance1, {
        method2: method2
        value4: 4
    });
    

    This will copy all the properties of the second argument to the object in the first argument. See the jQuery doc for more info.


    If you wanted to specify this as part of the constructor, you could do so like this and not have any additional lines of code to type:

    function myObject(value1, value2, value3, extraProperties){
        this.value1 = value1;
        this.value2 = value2;
        this.value3 = value3;
        this.method1 = method1;
        if (extraProperties) {
            jQuery.extend(this, extraProperties);
        }
    }
    

    In that case, you would just code this:

    var instance1 = new myObject(1, 2, 3, {method2: method2, value4: 4});
    

    Of course, the more Object Oriented way to do this would be to create prototypes for each type of object you want (some could inherit from others) and then just instantiate the right type of object rather than add instance-specific methods and properties.

    Or, create a single prototype that has all the methods/capabilities you might need on it for all the uses of the object rather than adding instance specific methods. In javascript, there is no penalty for having methods on an object that you don’t use sometimes if they are on the prototype. Things on the prototype are free per instance (no storage consumption per instance, no initialization cost per instance, etc…).

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.