简体   繁体   English

函数不返回值

[英]Function doesn't return the value

I have this function `我有这个功能`

let n = (index) => {
    if(index < 0){
        index = 0;  
    }
    return index;}

that must run inside of this function,必须在这个函数内运行,

prev.addEventListener('click', function() {  
    index--;
    n(index);
    console.log(index);
});

but for some reasons it wouldn't work.但由于某些原因它不起作用。 It looks like it sets the value of index, but actually it doesn't看起来它设置了索引的值,但实际上它没有

You are forgetting to set your index to the value returned by the function.您忘记将索引设置为函数返回的值。

prev.addEventListener('click', function() {  
    index--;
    index = n(index);
    console.log(index);
});

In here you have called the function n(index) but it's value isn't assigned anywhere, you have to assign it's value like below在这里你已经调用了函数n(index)但它的值没有在任何地方分配,你必须像下面这样分配它的值

let n = (index) => {
    if(index < 0){
        index = 0;  
    }
    return index;}

prev.addEventListener('click', function() {  
    index--;
    index = n(index); // This is where you have Assign it's Value
    console.log(index);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM