-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule4.js
More file actions
114 lines (82 loc) · 2.45 KB
/
Module4.js
File metadata and controls
114 lines (82 loc) · 2.45 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
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
113
114
//Task 1
function printOwnProperties(obj) {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
console.log(`${key}: ${obj[key]}`);
}
}
}
const person = {
name: "John",
age: 30,
occupation: "Developer"
};
printOwnProperties(person);
//Task 2
function checkProperty(str, obj){
return obj.hasOwnProperty(str);
}
const child = {
name: "John",
age: 10,
occupation: "Developer"
};
console.log(checkProperty("name", child));//true
console.log(checkProperty("family", child));//false
//Task 3
function createObject(){
return Object.create(null);
}
const emptyObj = createObject();
console.log(emptyObj);
console.log(Object.getPrototypeOf(emptyObj));
//Task 4
function ElectricalAppliance(name, power) {
this.name = name;
this.power = power;
}
ElectricalAppliance.prototype.plugIn = function() {
console.log(`${this.name} включен в розетку.`);
};
function DeskLamp(name, power) {
ElectricalAppliance.call(this, name, power);
}
DeskLamp.prototype = Object.create(ElectricalAppliance.prototype);
DeskLamp.prototype.constructor = DeskLamp;
function Computer(name, power) {
ElectricalAppliance.call(this, name, power);
}
Computer.prototype = Object.create(ElectricalAppliance.prototype);
Computer.prototype.constructor = Computer;
const deskLamp = new DeskLamp("Настольная лампа", 60);
const computer = new Computer("Компьютер", 500);
deskLamp.plugIn();
computer.plugIn();
const totalPower = deskLamp.power + computer.power;
console.log(`Потребляемая мощность: ${totalPower} Вт.`);
//Task 5
class ElectricalAppliance {
constructor(name, power) {
this.name = name;
this.power = power;
}
plugIn() {
console.log(`${this.name} включен в розетку.`);
}
}
class DeskLamp extends ElectricalAppliance {
constructor(name, power) {
super(name, power);
}
}
class Computer extends ElectricalAppliance {
constructor(name, power) {
super(name, power);
}
}
const deskLamp = new DeskLamp("Настольная лампа", 60);
const computer = new Computer("Компьютер", 500);
deskLamp.plugIn();
computer.plugIn();
const totalPower = deskLamp.power + computer.power;
console.log(`Потребляемая мощность: ${totalPower} Вт.`);