-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path53_OOP_constructor_functions.js
More file actions
39 lines (32 loc) · 1008 Bytes
/
53_OOP_constructor_functions.js
File metadata and controls
39 lines (32 loc) · 1008 Bytes
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
// This is a Constructor Function...
function Color(r, g, b) {
this.r = r;
this.g = g;
this.b = b;
}
//If you call it on its own like a regular function...
Color(35, 60, 190); //undefined
//It returns undefined. Seems useless!
// *****************
// THE NEW OPERATOR!
// *****************
// 1. Creates a blank, plain JavaScript object;
// 2. Links (sets the constructor of) this object to another object;
// 3. Passes the newly created object from Step 1 as the this context;
// 4. Returns this if the function doesn't return its own object.
Color.prototype.rgb = function() {
const { r, g, b } = this;
return `rgb(${r}, ${g}, ${b})`;
};
Color.prototype.hex = function() {
const { r, g, b } = this;
return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
};
Color.prototype.rgba = function(a = 1.0) {
const { r, g, b } = this;
return `rgba(${r}, ${g}, ${b}, ${a})`;
};
const color1 = new Color(40, 255, 60);
color1.hex();
const color2 = new Color(0, 0, 0);
color2.hex();