Need help properties within a property (javaScript)

Hi guys. I’m a newbie.

I’m trying to create an object called “person” with the properties firstName(string), lastName(string), age(number), occupation(string) and a property called “address” with properties: street (string), city(string), state(string) and zip(number).

all strings and numbers set to undefined

Could you post the code that you have tried?

1 Like

Something like this?


function Address(street, city, state) {
    this.street = street;
    this.city = city;
    this.state = state;
}

function Person(firstName, lastName, occupation, age, address) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.occupation = occupation;
    this.age = age;
   this.address = address;
}

var person = new Person("myFirst", "myLast", "myOccupation", 33, new Address("myStreet", "myCity", "myState"));

console.log(person);
1 Like

Your object works for me.
Try console.log() for your code in chrome’s console.

2 Likes

Your code works perfectly. Just rerun it.

1 Like

Thanks for all the input and fast responses!

I ended up doing:

var person = {
firstName: “”,
lastName: “”,
age: 35,
occupation: “”,
address: {
street: “”,
city: “”,
state: “”,
zip: 0
}
};
person.firstName = “Bob”
person.lastName = “BobAlso”
person.age = 35
person.occupation = “Simpleton”
person.address.street = “Elm Street”
person.address.city = “Nightmare”
person.address.state = “Television”
person.address.zip = 44444