Need help properties within a property (javaScript)

Need help properties within a property (javaScript)
0

#1

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).


#2

all strings and numbers set to undefined


#3

Could you post the code that you have tried?


#5

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);

#6

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


#7

Your code works perfectly. Just rerun it.


#8

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