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'use strict';
2
3var obj = {
4 a: 10
5 b:20
6};
7
8Object.defineProperty(obj, 'b', {
9 get: () => {
10 console.log(this.a, typeof this.a, this); // undefined 'undefined' Window {...} (or the global object)
11 return this.a + 10; // represents global object 'Window', therefore 'this.a' returns 'undefined'
12 }
13});
14
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");