Skip to content

Commit efe0dd4

Browse files
Object Literal vs Constructro in JS
1 parent 9f132f7 commit efe0dd4

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
// 51. Object Literal vs Constructor in JS
3+
4+
/*
5+
Object Property = key:value pair | name:value pair
6+
*/
7+
8+
var obj = {}
9+
console.log(obj) // {}
10+
console.log(typeof obj) // object
11+
12+
obj.x = 10
13+
console.log(obj) // { x: 10 }
14+
15+
// 1. Object Literal
16+
17+
var point = {
18+
x: 10,
19+
y: 20
20+
}
21+
console.log(point) // { x: 10, y: 20 }
22+
23+
point.y = 30
24+
point.z = 50
25+
console.log(point) // { x: 10, y: 30, z: 50 }
26+
27+
// 2. Object Constructor (Used in OOP)
28+
29+
var obj1 = Object()
30+
obj1.a = 10
31+
32+
var obj2 = new Object()
33+
obj2.b = 20
34+
35+
console.log(obj1) // { a: 10 }
36+
console.log(obj2) // { b: 20 }
37+
38+
39+

0 commit comments

Comments
 (0)