-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-prototype-comparison.js
More file actions
48 lines (39 loc) · 1.05 KB
/
Copy pathclass-prototype-comparison.js
File metadata and controls
48 lines (39 loc) · 1.05 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
'use strict'
class Button {
constructor(name) {
this.button = document.createElement("button");
this.button.innerHTML = name;
document.body.appendChild(this.button);
}
onClick(fn) {
this.button.onclick = fn;
}
}
const btn = new Button('Click Me')
btn.onClick(function() {
console.log('Clicked')
})
/* --------------- Function Prototype -------------- */
function Buttonfn(name) {
this.button = document.createElement("button");
this.button.innerHTML = name;
document.body.appendChild(this.button);
}
Buttonfn.prototype.onClickfn = function (fn) {
this.button.onclick = fn;
}
const btnfn = new Buttonfn('Click Fn')
btnfn.onClickfn(function() {
console.log('Clicked Fn')
})
/*
* Any function can convert into construtor, for that
* create an object for the function with the help of "new"
* Function name will act as Class name,
* code inside that fn considered as a constructor
*/
/*
* All function have prototype property
* If we add any function into base fn with the help of prototype property,
* then that fn is similar to class methods
*/