-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
65 lines (42 loc) · 1.14 KB
/
main.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const Person = function (firstName, birthYear) {
// console.log(this) // {}
this.firstName = firstName
this.birthYear = birthYear
// Never use this
// this.calcAge = function () {
// console.log(2022 - birthYear)
// }
}
Person.prototype.calcAge = function () {
// console.log(2037 - this.birthYear);
return 2037 - this.birthYear
}
const hamzah = new Person("Hamzah", 2000)
const areeb = new Person("Areeb", 2000)
const arham = new Person("Arham", 2000)
// 1. New {} is created
// 2. function is called, this = {}
// 3. {} linked to prototype
// 4. function automatically return object
// console.log(hamzah)
// console.log(hamzah.hasOwnProperty("calcAge"))
// class expression
// const PersonCl = class {
// constructor(firstName, birthYear) {
// this.firstName = firstName
// this.birthYear = birthYear
// }
// }
// class declaration
class PersonCl {
constructor(firstName, birthYear) {
this.firstName = firstName
this.birthYear = birthYear
}
clacAge() {
console.log(2037 - this.birthYear);
}
}
const ali = new PersonCl("Ali", 2000)
const hamzah2 = new PersonCl("Hamzah", 2000)
console.log(ali)