Javascript方法不返回变量

时间:2013-07-22 20:48:19

标签: javascript html

我对Javascript有点新鲜。我正在做一个工作项目,我在获取一个返回百分比的方法时遇到了一些麻烦。

function campaignProgress(goal, current){
    this.goal = goal;
    this.current = current; 
    this.percent =  Math.floor(current / goal  * 100);

    this.getGoal = function(){
        return goal;
    }
    this.getCurrent = function(){
        return current;
    }
    this.getPercent = function(){   
        return percent;
    }
}


var totalProgress = new campaignProgress(1.70, 1.064);

当我在html文件中调用它时,我在我的标题和我使用的正文中引用.js文件;

<script type="text/javascript">

        document.write(totalProgress.getGoal());

        document.write(totalProgress.getPercent());

</script>

getGoal()方法工作正常,但getPercent()不返回任何内容。我可以像这样引用百分比变量;

totalProgress.percent

它打印好了。关于为什么不起作用的任何建议都将不胜感激,谢谢。

5 个答案:

答案 0 :(得分:4)

您需要返回实例this的范围:

this.getGoal = function(){
    return this.goal;
}
this.getCurrent = function(){
    return this.current;
}
this.getPercent = function(){   
    return this.percent;
}
由于使用构造函数参数进行闭包,因此返回

goalcurrent。但是如果在构造函数运行后它们被更改,那么它们将返回错误的值。 percent变量显而易见。

答案 1 :(得分:2)

您将变量指定为函数的属性(this.goal),但是当您检索它们时,您正在尝试获取局部变量。这应该解决它:

function campaignProgress(goal, current){
    this.goal = goal;
    this.current = current; 
    this.percent =  Math.floor(current / goal  * 100);

    this.getGoal = function(){
        return this.goal;
    }
    this.getCurrent = function(){
        return this.current;
    }
    this.getPercent = function(){   
        return this.percent;
    }
}

这里的另一个问题是你使用new来创建这个“功能类”的实例吗?否则,分配this.goal将把这些变量分配到全局范围。

var c = campaignProgress(1, 3);
console.log(c);//undefined
console.log(window.goal);//1

VS

var c = new campaignProgress(1, 3)
console.log(c);//instance
console.log(window.goal);//undefined

答案 2 :(得分:2)

看起来你正试图模仿一个类,那里应该是“私人的”。我想你想要:

function campaignProgress(goal, current){
    var privateGoal = goal,
        privateCurrent = current;

    this.getGoal = function(){
        return privateGoal;
    };
    this.setGoal = function(g){
        privateGoal = g;
    };
    this.getCurrent = function(){
        return privateCurrent;
    };
    this.setCurrent = function(c){
        privateCurrent = c;
    };
    this.getPercent = function(){   
        return Math.floor(privateCurrent / privateGoal  * 100);
    };
}

var tp = new campaignProgress(1.70, 1.064);
console.log(tp.getGoal(), tp.getCurrent(), tp.getPercent());
tp.setCurrent(1.111);
console.log(tp.getGoal(), tp.getCurrent(), tp.getPercent());

DEMO: http://jsfiddle.net/twVNN/2/

这会导致privateGoalprivateCurrent为“私有”,这意味着无法在其范围之外访问它们。提供的方法允许通过调用它们来进行访问。如果您要使用this.goal = goal;之类的内容,则无需使用getGoalprivatePercent根据privateCurrentprivateGoal的值动态计算百分比。

答案 3 :(得分:2)

您需要使用闭包var that = this;

function campaignProgress(goal, current) {
    this.goal = goal;
    this.current = current; 
    this.percent =  Math.floor(current / goal  * 100);

    var that = this;
    this.getGoal = function() {
        return that.goal;
    }
    this.getCurrent = function(){
        return that.current;
    }
    this.getPercent = function(){   
        return that.percent;
    }
}


var totalProgress = new campaignProgress(1.70, 1.064);
console.log(totalProgress.getGoal());
console.log(totalProgress.getPercent());

这总是函数()中的值,如果你在哪里调用this.goal(如上所述),那就像调用目标一样

答案 4 :(得分:0)

目标有效,因为目标是在每个函数的闭包中定义的(传递给函数的目标参数)。其他的不起作用,因为他们没有用关键字引用它们(这几乎不像某些语言那样可选)。不幸的是,由于它的工作方式,我们不能简单地将它添加到每个子函数的return语句中(因为我们没有处理原型,它的值可能会根据我们的位置而改变从中调用这些函数。因此,请使用粗体更改。

function campaignProgress(goal, current){
    var self = this;
    this.goal = goal;
    this.current = current; 
    this.percent =  Math.floor(current / goal  * 100);

    this.getGoal = function(){
        return self.goal;
    }
    this.getCurrent = function(){
        return self.current;
    }
    this.getPercent = function(){   
        return self.percent;
    }
}

通过使用自变量,我们在第一次调用函数时捕获了我们想要的值,然后我们对它进行操作。