Javascript方法未返回变量
我对Javascript有点陌生。我正在做一个工作项目,在获取返回百分比的方法时遇到了一些问题Javascript方法未返回变量,javascript,html,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;
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>
它会很好地打印出来。如果您能提供有关此操作不起作用的任何建议,我们将不胜感激。您需要通过实例的范围返回
this
:
this.getGoal = function(){
return this.goal;
}
this.getCurrent = function(){
return this.current;
}
this.getPercent = function(){
return this.percent;
}
goal
和current
由于构造函数参数的闭包而返回。但是如果在构造函数运行后更改它们,那么它们将返回错误的值。这在百分比
变量中很明显。您将变量指定为函数的属性(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
看起来您正试图模拟一个类,其中,这些类应该是“私有的”。我想你会想要:
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());
演示:
这会导致
privateGoal
和privateCurrent
为“private”,这意味着不能在其范围外访问它们。提供的方法通过调用它们来允许访问。使用this.goal=goal如果要使用类似于getGoal
的内容,则不需要使用code>。privatePercent
根据privateCurrent
和privateGoal
的值动态计算百分比,您需要使用closuresvar=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(如上所述),这与简单地调用goal是一样的,因为goal是在每个函数的闭包(传递到函数中的goal参数)中定义的。其他的不起作用,因为它们没有使用关键字this引用(这几乎不像某些语言那样是可选的)。不幸的是,由于它的工作方式,我们不能简单地将它添加到每个子函数的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;
}
}
通过使用self变量,我们在第一次调用函数时捕获了我们想要的值,然后对其进行操作。您收到的任何控制台消息?百分比未定义,您想要这个.percent,它会破坏命名参数。@dandavis您在说什么?您不应该重新声明(var)以前声明的变量,在这种情况下,目标和当前目标。@dandavis我不会争论,我会改变并接受你的建议。以前使用它并没有什么坏处,但这是不必要的。它只是一个糟糕的形式,即使它起作用了,还有一些旧的浏览器边缘实例,当形式和词汇重叠时,提升模式可能会导致问题。@dandavis否,它们是实例的属性(totalProgress
),不是局部变量。@dandavis但是如果我们想通过这个访问它们呢?@KonstantinD Infrasgistics很有效,谢谢。“现在看看你的解释就很有道理了。”康斯坦丁基础设施公司我投票否决了你,而不是丹达维斯。因为这是不必要的。无论如何,这个
的值不能保证是那些函数中所期望的值。我怀疑这是OP(或任何人)想要的。为什么你要声明一个“公共”属性,然后用“公共”方法来获取它们?@KonstantinD Infragistics:我从来没有给任何人投票过。看看我的徽章。我说的是OP的代码,其中不需要this.current…最好使用它,因为它们被设置为属性。这意味着它们可能在其他地方被修改。只要访问本地变量,他们就不会知道属性的更改。因为OP使用的是getter,所以这没什么关系。@Teemu您不需要在这里使用那
,但是这
不能保证是您在这些方法中所期望的。如果您执行了var cp=new-activationprogress(1,2),method=cp.getGoal;警报(方法())代码>?这个答案解决了问题that@Teemu这里我所说的闭包(可能不是正确的术语)是指函数中的这个引用了该特定函数的开始括号和结束括号之间的内容。@Teemu-Ahh我明白了,我不确定我是否遗漏了这个或什么:)好吧,返回这个.XXXX代码>仅在构造函数内部工作。我刚刚意识到@Ian刚刚评论了什么。当脱离上下文时,我们将得到未定义的
。因此,在内部函数中设置var=this
并使用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());
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;
}
}