[英]Boolean not updating it's value when updated in a function
I have created a function called showMoreImages which accepts state as a parameter to check if it is true then change it's value to false along with some other operations. 我创建了一个名为showMoreImages的函数,该函数接受state作为参数来检查其是否为true,然后将其值与其他一些操作一起更改为false。 However when I tried it does not update the boolean value referenced.
但是,当我尝试它不会更新引用的布尔值。 Below the code snippet.
下面的代码段。
function showMoreImages(num, el, category, state){
for (var i = 0; i < category.length; i++) {
toggleClass('show-images', category[i]);
toggleClass('hide-images', category[i]);
}
if(state) {
updateState('shift-down', 'shift-up', image_one[num]);
el.innerHTML='View Less';
state=false;
}else{
updateState('shift-up', 'shift-down', image_one[num]);
el.innerHTML='View More';
state=true;
}
}
Where the function is called: 调用函数的位置:
if (i==1) {
var isPlacesClose=true;
buttons[i].addEventListener('click', function(){
showMoreImages(0, this, places, isPlacesClose);
});
}
if (i==2) {
var isPeopleClose=true;
buttons[i].addEventListener('click', function(){
showMoreImages(1, this, people, isPeopleClose);
});
}
if (i==3) {
var isEventsClose=true;
buttons[i].addEventListener('click', function(){
showMoreImages(2, this, events, isEventsClose);
});
}
What I am trying to achieve is to update the boolean value which is passed as a parameter. 我正在尝试实现的是更新作为参数传递的布尔值。 For instance where i=1 I want to update isPlacesClose to false and vice versa.
例如,如果i = 1,我想将isPlacesClose更新为false,反之亦然。
Thanks in advance! 提前致谢!
state
is a primitive value, when its value is changed in showMoreImages
, it is not retained outside the scope of showMoreImages
state
是一个原始值,当它的值在showMoreImages
更改时, 不会保留在showMoreImages
范围showMoreImages
Return the value instead 传回值
function showMoreImages(num, el, category, state){
for (var i = 0; i < category.length; i++) {
toggleClass('show-images', category[i]);
toggleClass('hide-images', category[i]);
}
if(state) {
updateState('shift-down', 'shift-up', image_one[num]);
el.innerHTML='View Less';
return false; //observe change in this line, value is returned instead of setting the same in state
}else{
updateState('shift-up', 'shift-down', image_one[num]);
el.innerHTML='View More';
return true; //observe change in this line, value is returned instead of setting the same in state
}
}
Now receive the returned value in the variable which was passed in the argument 现在,在参数中传递的变量中接收返回值
if (i==1) {
var isPlacesClose=true;
buttons[i].addEventListener('click', function(){
//observe change in this line, isPlacesClose is updated with returned value
isPlacesClose = showMoreImages(0, this, places, isPlacesClose);
});
}
if (i==2) {
var isPeopleClose=true;
//observe change in this line, isPeopleClose is updated with returned value
buttons[i].addEventListener('click', function(){
isPeopleClose = showMoreImages(1, this, people, isPeopleClose);
});
}
if (i==3) {
var isEventsClose=true;
buttons[i].addEventListener('click', function(){
isEventsClose = showMoreImages(2, this, events, isEventsClose);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.