-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path48.js_oop.js
More file actions
120 lines (77 loc) · 2.04 KB
/
Copy path48.js_oop.js
File metadata and controls
120 lines (77 loc) · 2.04 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
115
116
// // Example 331 - In JS objects property are not copied like it does in
// // real Object Oriented Programming Language
// function extend(source, destination) {
// for(var key in source) {
// if(!(key in destination)) { // if key is not available in destination
// destination[key] = source[key];
// }
// }
//
//
// return destination;
// }
//
//
//
//
// var File = {
// name: 'hello',
// size: 4545144,
// extension: 'mp3',
// getName: function() {
// return this.name + '.' + this.extension;
// },
// description: function() {
// console.log('FileName:', this.getName());
// console.log('FileType:', this.extension);
// console.log('FileSize:', this.size);
// }
// };
//
//
// // extending File
// var MusicFile = extend(File, {
// length: 356,
// play: function () {
// console.log("playing " + this.getName());
// },
// description: function() {
// File.description.call(this);
// console.log('Length:', this.length);
// }
// });
//
//
// File.description();
// MusicFile.description();
// Example 332 - Traditional JS Classes
function File() {
this.name = 'hello';
this.extension = 'mp3';
this.size = 45454;
this.getName = function() {
return this.name + '.' + this.extension;
}
this.description = function() {
console.log('FileName:', this.getName());
console.log('FileType:', this.extension);
console.log('FileSize:', this.size);
};
}
function MusicFile() {
var musicFile = new File();
musicFile.length = 545;
// save a reference to Vehicle::description
fileDescription = musicFile.description;
// Vehicle::description override
musicFile.description = function() {
fileDescription.call(this);
console.log('Length:', this.length);
};
return musicFile;
};
var genericFile = new File();
genericFile.description();
console.log();
var helloMusic = new MusicFile();
helloMusic.description();