[英]Change var in object literal function
Hi guys I am writing some code using the object literal pattern, I have function that returns a value: 嗨,大家好,我正在使用对象文字模式编写一些代码,我有一个返回值的函数:
'currentLocation': function() {
var cL = 0;
return cL;
},
I then need to update the variable 'cL' from another function like this: 然后,我需要从另一个函数中更新变量“ cL”,如下所示:
teamStatus.currentLocation() = teamStatus.currentLocation() + teamStatus.scrollDistance();
This part is part of another function - however I get an error back stating: invalid assignment left-hand side 这部分是另一个功能的一部分-但是我得到一个错误,指出:左侧的赋值无效
I am guessing I can not update the variable in this way, could anyone suggest a better method or point me in the right direction. 我猜我不能以这种方式更新变量,有人可以建议一种更好的方法还是可以向我指出正确的方向。
Any help would be greatly appreciated. 任何帮助将不胜感激。
Going to add more code to highlight what I am trying to do: 将添加更多代码以突出显示我要执行的操作:
'currentLocation': function() {
var cL = 0;
return cL;
},
'increaseTable': function() {
if (teamStatus.currentLocation() <= teamStatus.teamStatusTableHeight() ) {
teamStatus.currentLocation = teamStatus.currentLocation() + teamStatus.scrollDistance();
$("#tableTrackActual").animate({scrollTop: (teamStatus.currentLocation)});
$("#tableMembers").animate({scrollTop: (teamStatus.currentLocation) });
//console.log(teamStatus.currentLocation());
teamStatus.buttonRevealer();
}
}
As you can see increaseTable should update the value of currentLocation - help this sheds more light on what I am trying to achieve. 如您所见,increaseTable应该更新currentLocation的值-帮助这使我更清楚地了解要实现的目标。
You're writing teamStatus.currentLocation() =
, which calls the function teamStatus.currentLocation
and tries to assign to the return value. 您正在编写
teamStatus.currentLocation() =
,它将调用函数teamStatus.currentLocation
并尝试分配给返回值。 That isn't valid. 那是无效的。 You want just
teamStatus.currentLocation =
— no function call. 您只需要
teamStatus.currentLocation =
-没有函数调用。
The variable inside your function is completely private to that function (and any functions defined within it). 函数内部的变量完全是该函数(及其中定义的任何函数)的专用。 If you need to create a number of functions that share a set of private variables, you can do that with a closure.
如果您需要创建多个共享一组私有变量的函数,则可以使用闭包来实现。 For instance:
例如:
var Thing = (function() {
var thingWideData;
function getData() {
return thingWideData;
}
function setData(newData) {
thingWideData = newData;
}
return {
getData: getData,
setData: setData
};
})();
What that does is create a Thing
object which has getData
and setData
functions available for it, which get and set the completely private thingWideData
variable contained by the anonymous closure. 要做的是创建一个
Thing
对象,该对象具有可用的getData
和setData
函数,它们获取并设置匿名闭包中包含的完全私有的 thingWideData
变量。 More about this pattern here and here , although the latter of those is more about private methods than private data. 尽管这里的后者更多地是关于私有方法而不是私有数据,但是这里和这里更多地关于这种模式。
What your code produces is: 您的代码产生的是:
0 = 0 + <some number>
Which variable do you want to update? 您要更新哪个变量?
cL
? cL
? You are declaring it in the function, you cannot assign a value to it from outside. 您在函数中声明了它,您无法从外部为其分配值。 Depending on the rest of your code, you might be better off with
getters and setters
: 根据代码的其余部分,使用
getters and setters
可能会更好:
var object = {
_cL = 0,
get currentLocation() {
return this._cL;
},
set currentLocation(value) {
this._cL = value;
}
}
then you can do: 那么您可以执行以下操作:
teamStatus.currentLocation = teamStatus.currentLocation + teamStatus.scrollDistance();
Update: 更新:
Regarding IE: If currentLocation
should actually be just a number, it might be sufficient to just declare it as property: 关于IE:如果
currentLocation
实际上应该只是一个数字,仅将其声明为属性可能就足够了:
var obj = {
currentLocation: 0
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.