Help me understand why I can't do the following:
function rgb(r = 0, g = 0, b = 0) {
this.r = r % 255
this.g = g % 255
this.b = b % 255,
this.str = function() {
return `rgb(${this.r}, ${this.g}, ${this.b})`
}
}
function rect(w, h, x, y, rgb = new rgb()) {
this.dim = {
w: w,
h: h
}
this.pos = {
x: x,
y: y
}
this.rgb = rgb
}
I get the following error (when calling rect()
):
ReferenceError: can't access lexical declaration `rgb' before initialization
function rgb(r = 0, g = 0, b = 0) {
this.r = r % 255
this.g = g % 255
this.b = b % 255,
this.str = function() {
return `rgb(${this.r}, ${this.g}, ${this.b})`
}
}
function rect(w, h, x, y, rgb = new rgb()) {
this.dim = {
w: w,
h: h
}
this.pos = {
x: x,
y: y
}
this.rgb = rgb
}
const r = new rect(1, 2, 3, 4);
Both rgb
and rect
are defined in the same file, and rgb
is defined before rect
.