I have two methods in my class. One method randomizes values, another one returns those values in a string that represents CSS color. Yet after randomization it still returns same string all the time, even though values in created object are changed. Can you please explain why?
Here's jsfiddle: https://jsfiddle.net/wpem1j9e/
function Color(r, g, b) {
this.r = r;
this.g = g;
this.b = b;
this.randomize = function(){
this.r = Math.floor(Math.random() * 100);
this.g = Math.floor(Math.random() * 100);
this.b = Math.floor(Math.random() * 100);
return this;
}
this.toString = function(){
var cssColor = "rgb(" + r + "%," + g + "%," + b + "%)";
return cssColor;
}
}
var color1 = new Color (1, 1, 1);
color1.randomize().toString(); // still returns "rgb(1%,1%,1%)";
r,g, andbin yourtoStringmethod.