Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 重复变量笑脸_Javascript - Fatal编程技术网

Javascript 重复变量笑脸

Javascript 重复变量笑脸,javascript,Javascript,我在Khan Academy的节目中遇到了重复变量的问题。程序似乎运行良好,但消息提示“两个或多个变量具有相同的名称。作为最佳实践,您应该为变量使用不同的名称。”我一辈子都搞不懂这一点 var SmileyFace = function(centerX, centerY) { this.centerX = centerX; this.centerY = centerY; }; SmileyFace.prototype.draw = fun

我在Khan Academy的节目中遇到了重复变量的问题。程序似乎运行良好,但消息提示“两个或多个变量具有相同的名称。作为最佳实践,您应该为变量使用不同的名称。”我一辈子都搞不懂这一点

    var SmileyFace = function(centerX, centerY) {
        this.centerX = centerX;
        this.centerY = centerY;
    };

    SmileyFace.prototype.draw = function() {
        fill(255, 234, 0);
        ellipse(this.centerX, this.centerY, 150, 150);
        fill(0, 0, 0);
        ellipse(this.centerX-30, this.centerY-30, 20, 20);
        ellipse(this.centerX+30, this.centerY-30, 20, 20);
        noFill();
        strokeWeight(3);
        arc(this.centerX, this.centerY+10, 64, 40, 0, 180);
    };

    var face = new SmileyFace(200,300);
    face.draw();

    SmileyFace.prototype.speak = function(phrase) {
        text(phrase, this.centerX+100, this.centerY, 100, 100);
    };

    var Smiley = new SmileyFace(100, 300);
    face.draw();
    face.speak("hello");

    var face = new SmileyFace(100, 100);
    face.draw();
    face.speak("Yolo");

    var face = new SmileyFace (90,200);
    face.draw();
    face.speak("Hi");

您已经声明并初始化了3次
face
变量

var face = new SmileyFace(200,300);
var face = new SmileyFace(100, 100);
var face = new SmileyFace (90,200);

给他们起不同的名字。可能是
face1、face2和face3

您将重新说明
face
变量3次。如果您只想重新分配,则无需重新声明:

var face = new SmileyFace(200,300);
// ...

face = new SmileyFace(100, 100);
// ...
或者,为每个实例使用一个新变量:

var face1 = new SmileyFace(200,300);
// ...

var face2 = new SmileyFace(100, 100);
// ...

尝试为
face
变量使用不同的名称,如
face\u你好,face\u你好