-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_objects_inheritance.js
114 lines (88 loc) · 3.23 KB
/
04_objects_inheritance.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Primitive value: string, number, boolean, null, undefined
// Object: myObject --> Object.prototype --> null
// Array: myArray --> Array.prototype --> Object.prototype --> null
// Function: myFunc --> Function.prototype --> Object.prototype --> null
// String: myString --> String.prototype --> Object.prototype --> null
// Number: myNumber --> Number.prototype --> Object.prototype --> null
// Boolean: myBoolean --> Boolean.prototype --> Object.prototype --> null
const product = 'Computer'
console.log(product)
const otherProduct = new String('Phone')
console.log(otherProduct)
//------------------------------------------------------------------------------------------------------------------------------
const Product = {
name1: 'test'
}
console.log(Product.hasOwnProperty('name1'))
console.log(Product.hasOwnProperty('name2'))
//------------------------------------------------------------------------------------------------------------------------------
Object.prototype.someNewMethod = () => 'this is a new function which is now available for all objects as all objects extend from object class'
const Product2 = {}
console.log(Product2.someNewMethod());
//------------------------------------------------------------------------------------------------------------------------------
class Person {
constructor(firstName, lastName, age, likes = []) {
this.firstName = firstName
this.lastName = lastName
this.age = age
this.likes = likes
}
getBio() {
let bio = `${this.firstName} is ${this.age}.`
this.likes.forEach((like) => {
bio += ` ${this.firstName} likes ${like}.`
})
return bio
}
setName(fullName) {
const names = fullName.split(' ')
this.firstName = names[0]
this.lastName = names[1]
}
}
class Employee extends Person {
constructor(firstName, lastName, age, position, likes) {
super(firstName, lastName, age, likes)
this.position = position
}
getBio() {
return `${this.firstName} ${this.lastName} is a ${this.position}.`
}
getYearsLeft() {
return 65 - this.age
}
}
class Student extends Person {
constructor(firstName, lastName, age, grade, likes) {
super(firstName, lastName, age, likes)
this.grade = grade
}
updateGrade(change) {
this.grade += change
}
getBio() {
const status = this.grade >= 70 ? 'passing' : 'failing'
return `${this.firstName} is ${status} the class.`
}
}
const me = new Student('Andrew', 'Mead', 27, 88, [])
console.log(me.getBio())
me.updateGrade(-20)
console.log(me.getBio())
//------------------------------------------------------------------------------------------------------------------------------
//getters and setters for objects
const data = {
locations: [],
get location() {
return this._location
},
set location(value) {
this._location = value.trim()
this.locations.push(this._location)
}
}
// code that uses the data object
data.location = ' Philadelphia '
data.location = ' New York'
console.log(data)
//------------------------------------------------------------------------------------------------------------------------------