1

For example:

function userObject(start_value) {  
    this.name = start_value;
    this.address = start_value;
    this.cars = function() {
        this.value = start_value;
        this.count = start_value;
    };
}

Obviously the above dosent work but would appreciate the direction to take to have cars available as: userObject.cars.value = 100000;

Cheers!

2 Answers 2

5

Remember that functions (and thus "object definitions") can be nested (there is absolutely no requirement that this is nested, but having it nested here allows a closure over start_value):

function userObject(start_value) {  
    this.name = start_value;
    this.address = start_value;
    function subObject () {
        this.value = start_value;
        this.count = start_value;
    }
    this.cars = new subObject();
}

However, I would likely opt for this (just create a new "plain" Object):

function userObject(start_value) {  
    this.name = start_value;
    this.address = start_value;
    this.cars = {
        value: start_value,
        count: start_value
    };
}

Happy coding.

Sign up to request clarification or add additional context in comments.

1 Comment

this.cars = { value: start_value, count: start_value }; did the trick. Thank you.
1

so simple anser is to use the object syntax

function userObject(start_value) {  
 this.name = start_value;
 this.address = start_value;
 this.cars = {
  value: start_value,
  count: start_value
 };
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.