-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructor-functions.js
More file actions
50 lines (40 loc) · 1.01 KB
/
constructor-functions.js
File metadata and controls
50 lines (40 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// uses this
function Person(name) {
this.name = name
}
const person = new Person('Alice')
function Counter() {
this.count = 0 // public by default
this.increment = function() {
this.count++
}
this.getCount = function() {
return this.count
}
}
const counter = new Counter()
counter.increment()
counter.increment()
console.log(counter.getCount())
function Book(title, author, year) {
this.title = title
this.author = author
this.year = year
this.getInfo = function() {
return `${this.title}, by ${this.author} , ${this.title}`
}
}
const book1 = new Book("Java", "Petros", "2025")
console.log(book1.getInfo())
class BookClass {
constructor (title, author, year) {
this.title = title
this.author = author
this.year = year
this.getInfo = function() {
return `${this.title}, by ${this.author} , ${this.title}`
}
}
}
const book2 = new BookClass("JS", "Petros", "2025")
console.log(book2.getInfo())