Skip to content

Commit 384aa6a

Browse files
authored
Objects in JS
1 parent 110e6d4 commit 384aa6a

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

objects.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
console.log("Objects in JavaScript");
2+
// Objects in JS can be created using 2 methods:
3+
// i) Using Literals
4+
// ii) Using Constructor
5+
6+
// Using Literals
7+
// Using Literals only one object can be created at a time so this method is sometimes inefficient
8+
let myObj1 = {
9+
// This Object contains some information about a BOOK
10+
name: "General Physics",
11+
author: "I.E Irodov",
12+
published: 2020,
13+
description: "Unavailable",
14+
};
15+
console.log(myObj1);
16+
console.log(myObj1.name);
17+
18+
console.log("********************************************************");
19+
// Using Constructors
20+
function objConst(name, author, published) {
21+
// This Object contains some information about BOOKS
22+
this.name = name;
23+
this.author = author;
24+
this.published = published;
25+
}
26+
// We just made a constructor for an obejct
27+
// Create a new Object using this constructor
28+
let book1 = new objConst("NCERT", "Unknown", 2021);
29+
let book2 = new objConst("C for Everyone", "Yashwant Kanetkar", 2004);
30+
console.log(book1);
31+
console.log(book2);

0 commit comments

Comments
 (0)