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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:48:33+00:00 2026-05-29T07:48:33+00:00

I have a couple of javascript objects that each have a setup method. The

  • 0

I have a couple of javascript objects that each have a setup method. The code is identical for all of them so I created a function called setupMain. Then for each instance of the object I’m trying to set it’s setup value to setupMain. something like below… but when I look at the setup value after an instance is created, it’s coming back undefined instead of pointing to the setupMain function. Any idea why? Thanks.

var customObject = function(){
  this.title = "";
}
var setupMain = function(obj){
  obj.title = "initial setup value";
}

var co = new customObject();
co.setup = setupMain(co);
  • 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-29T07:48:34+00:00Added an answer on May 29, 2026 at 7:48 am

    You might be looking for something like this:

    var customObject = function(){
      this.title = "";
    }
    var setupMain = function(){ //"This" will point to instance, such as co
      this.title = "initial setup value";
    }
    
    var co = new customObject();
    co.setup = setupMain; //Reference to function
    
    co.setup(); //Call the setup function
    window.alert(co.title);
    

    Also, if you don’t want to have to keep setting the setup function each time to create an instance, you can move it to the prototype:

    customObject.prototype.setup = setupMain; //Now, every customObject has a setup function
    var co = new customObject();
    co.setup();
    window.alert(co.title);
    

    Finally, if you didn’t want to have to call setup(); each time, you can call setup within the constructor:

    var customObject = function(){
      this.setup(); //Call shared setupMain function because it's part of the prototype
    }
    var setupMain = function(){
      this.title = "initial setup value";
    }
    
    customObject.prototype.setup = setupMain; //This can be shared across many prototypes
    
    var co = new customObject();
    window.alert(co.title);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My page deals with many Store objects, each of them has a field called
I have a couple of standard ASP.NET web methods that I'm calling from javascript
So I have code like this: <script language=javascript> $(function(){ function hellothere(strng){ showalert(strng); } function
I have an HTML page with a couple javascript buttons that 'replace' the HTML/CSS.
I have a page that references a couple of externally hosted javascript files -
I have couple resource DLLs that I currently load when application starts using following
I have couple of questions about AS3 variables handling by AVM/compiler/scope .1. This code
I'll have couple of python functions I must interface with from the assembly code.
ok I have couple of .NET classes that I want to use in VBA.
I have a couple of files containing a value in each line. EDIT :

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.