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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:05:35+00:00 2026-05-21T09:05:35+00:00

I have a object like this: myobj={ //where to define Car car=new Car(); test:{

  • 0

I have a object like this:

myobj={
  //where to define Car car=new Car();
  test:{
    testFunc1(){car.paintTest();},
    testFunc2(){car.drawTest();}
  },

  use:{
    useFunc1(){car.paintUse();},
    useFunc2(){car.drawUse();}
  },
};

I will use myobj in the way that, in another js file, I can easily call

myobj.test.testFunc2();

or

myobj.use.useFunc1();

As you notice, in myobj, I have a car instance frequently be used in different functions in different sub-objects (e.g. “test”, “use” sub-objects).

I am wondering, where can I define Car car=new Car(); in myobj so that the car is defined once and can be used in several functions in myobj.

  • 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-21T09:05:35+00:00Added an answer on May 21, 2026 at 9:05 am

    you can do that like this:

    myobj={
      car: new Car(),
    
      test: {
        var self = this;
        testFunc1(){self.car.paintTest();},
        testFunc2(){self.car.drawTest();}
      },
    
      use: {
        var self = this;
        useFunc1(){self.car.paintUse();},
        useFunc2(){self.car.drawUse();}
      },
    };
    

    UPDATE

    working version with fake Car object:

    function Car (){
        this.paintTest = function(){console.log('painttest')}
        this.drawTest = function(){console.log('drawtest')}
        this.paintUse = function(){console.log('paintuse')}
        this.drawUse = function(){console.log('drawuse')}
    }
    
    myobj = {
      car: new Car(),
    
      test:{
          testFunc1: function() {myobj.car.paintTest();},
          testFunc2: function() {myobj.car.drawTest();}
      },
    
      use: {
          useFunc1: function(){myobj.car.paintUse();},
          useFunc2: function(){myobj.car.drawUse();}
      },
    };
    
    myobj.test.testFunc1();
    myobj.test.testFunc2();
    myobj.use.useFunc1();
    myobj.use.useFunc2();
    

    fiddle: http://jsfiddle.net/maniator/KcQvL/

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

Sidebar

Related Questions

I have a line of code like this: var myObj:[SomeClass] = new [SomeClass](); I
Suppose I have a design like this: Object GUI has two objects: object aManager
I have a Linux C++ application and I'd like to test an object pointer
I have code like this var MyObj = { f1 : function(o){ o.onmousedown =
I have an object like this: TMyObj = class private FObjList: TObjectDictionary <integer, TMyObject>;
I have a class like this: class MyBase(object): x = 3 Documentation for property
I have two classes declared like this: class Object1 { protected ulong guid; protected
I have an object tree that looks something like Ball / \ LegalBall IllegalBall
When using something like object.methods.sort.to_yaml I'd like to have irb interpret the \n characters
I'm creating my own JavaScript Array-like object and I have methods that call closures.

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.