Javascript方法总是返回相同的值

Javascript方法总是返回相同的值,javascript,Javascript,我班上有两种方法。一个方法随机化值,另一个方法返回表示CSS颜色的字符串中的值。然而,在随机化之后,它仍然始终返回相同的字符串,即使创建的对象中的值发生了更改。你能解释一下原因吗 下面是JSFIDLE: 因为在toString函数中,您使用的是r、g和b而不是this.r、this.g和this.b。前者是构造函数参数。因为在toString函数中使用的是r、g和b而不是this.r、this.g和this.b。前者是构造函数参数。函数颜色(r、g、b){ function Color(r, g

我班上有两种方法。一个方法随机化值,另一个方法返回表示CSS颜色的字符串中的值。然而,在随机化之后,它仍然始终返回相同的字符串,即使创建的对象中的值发生了更改。你能解释一下原因吗

下面是JSFIDLE:


因为在toString函数中,您使用的是
r
g
b
而不是
this.r
this.g
this.b
。前者是构造函数参数。

因为在toString函数中使用的是
r
g
b
而不是
this.r
this.g
this.b
。前者是构造函数参数。

函数颜色(r、g、b){
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(" + this.r + "%," + this.g + "%," + this.b + "%)"; //new
        return cssColor;
    }
}
var color1 = new Color (1, 1, 1);
output = function () {
    var outputDiv = document.getElementById("output");
    outputDiv.innerHTML = outputDiv.innerHTML + "<br>" + color1.r + "   " + color1.g + "   " + "   " + color1.b + "<br>" + color1.toString() + "<br>";
}
output();
document.getElementById("input").onclick = function(){
    color1.randomize();
  output();
};
这个。r=r; 这个.g=g; 这个.b=b; this.randomize=函数(){ this.r=Math.floor(Math.random()*100); this.g=Math.floor(Math.random()*100); this.b=Math.floor(Math.random()*100); 归还这个; } this.toString=函数(){ var cssColor=“rgb(“+this.r+”、“+this.g+”、“+this.b+”)”;//新建 返回cssColor; } } var color1=新颜色(1,1,1); 输出=函数(){ var outputDiv=document.getElementById(“输出”); outputDiv.innerHTML=outputDiv.innerHTML+“
”+color1.r+“+color1.g+”+“+color1.b+”
“+color1.toString()+”
”; } 输出(); document.getElementById(“输入”).onclick=function(){ 颜色1.随机化(); 输出(); };
功能颜色(r、g、b){
这个。r=r;
这个.g=g;
这个.b=b;
this.randomize=函数(){
this.r=Math.floor(Math.random()*100);
this.g=Math.floor(Math.random()*100);
this.b=Math.floor(Math.random()*100);
归还这个;
}
this.toString=函数(){
var cssColor=“rgb(“+this.r+”、“+this.g+”、“+this.b+”)”;//新建
返回cssColor;
}
}
var color1=新颜色(1,1,1);
输出=函数(){
var outputDiv=document.getElementById(“输出”);
outputDiv.innerHTML=outputDiv.innerHTML+“
”+color1.r+“+color1.g+”+“+color1.b+”
“+color1.toString()+”
”; } 输出(); document.getElementById(“输入”).onclick=function(){ 颜色1.随机化(); 输出(); };
您只需放弃使用
这个
,就可以减轻很多困惑。它在这里没有好处

功能颜色(r、g、b){
变量me={
随机化:函数(){
r=数学地板(数学随机()*100);
g=数学地板(数学随机()*100);
b=数学地板(数学随机()*100);
还我;
},
toString:function(){
var cssColor=“rgb(“+r+”%,“+g+”%,“+b+”%)”;
返回cssColor;
}
};
还我;
}
var color1=颜色(1,1,1);

log(color1.randomize().toString())您只需放弃使用
这个
,就可以减轻很多混乱。它在这里没有好处

功能颜色(r、g、b){
变量me={
随机化:函数(){
r=数学地板(数学随机()*100);
g=数学地板(数学随机()*100);
b=数学地板(数学随机()*100);
还我;
},
toString:function(){
var cssColor=“rgb(“+r+”%,“+g+”%,“+b+”%)”;
返回cssColor;
}
};
还我;
}
var color1=颜色(1,1,1);

log(color1.randomize().toString())
调用变量时,toString函数中缺少“this”。Sybrodsky指的是在
toString
方法中使用
r
g
b
的位置。缺少“this”调用变量时,在toString函数中,Sybrodsky指的是您在
toString
方法中使用
r
g
b
的位置。哇,我花了大约一个小时试图弄清楚!令人尴尬的非常感谢。当时间限制结束时,我会接受你的答案。哦,哇,我花了大约一个小时试图弄明白!令人尴尬的非常感谢。时限结束时我会接受你的答复。
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(" + this.r + "%," + this.g + "%," + this.b + "%)"; //new
        return cssColor;
    }
}
var color1 = new Color (1, 1, 1);
output = function () {
    var outputDiv = document.getElementById("output");
    outputDiv.innerHTML = outputDiv.innerHTML + "<br>" + color1.r + "   " + color1.g + "   " + "   " + color1.b + "<br>" + color1.toString() + "<br>";
}
output();
document.getElementById("input").onclick = function(){
    color1.randomize();
  output();
};