1var obj = {
2 prop1: 5,
3 obj2: {
4 prop1: [3, 6, 3],
5 prop2: 74,
6 prop3: {
7 str: "Hello World"
8 }
9 }
10};
11
12console.log(obj.obj2.prop3.str); //output: "Hello World"
1person = {
2 'name':'john smith'
3 'age':41
4};
5
6console.log(person);
7//this will return [object Object]
8//use
9console.log(JSON.stringify(person));
10//instead
1// To make an object literal:
2const dog = {
3 name: "Rusty",
4 breed: "unknown",
5 isAlive: false,
6 age: 7
7}
8// All keys will be turned into strings!
9
10// To retrieve a value:
11dog.age; //7
12dog["age"]; //7
13
14//updating values
15dog.breed = "mutt";
16dog["age"] = 8;
1function Dog() {
2 this.name = "Bailey";
3 this.colour = "Golden";
4 this.breed = "Golden Retriever";
5 this.age = 8;
6}
1//initialized object
2var object = {
3 property1: "a",
4 property2: "b",
5}
6
7//object initializer function
8function object(property1, property2)
9{
10 this.property1 = property1;
11 this.property2 = property2;
12}
13
14//initializing using the initializer function
15var mouse = new object("ab", "cd");