简体   繁体   English

Function 不返回值

[英]Function does not return the value

function Increment()
{
  var counter = 100;
   function inner() 
    {
         counter += counter;
         return counter;
    }
}
var x = Increment();
console.log(x);
console.log(x);
console.log(x);

This is instruction of this code Use JavaScript Closure/self invoking method to do the following: a)Name the outer function as 'Increment”.这是此代码的说明 使用 JavaScript 闭包/自调用方法执行以下操作: a) 将外部 function 命名为“增量”。 b)Store 100 as a counterin the outer function. b) 将 100 作为计数器存储在外部 function 中。 c)Increment the counter by 100 in the inner functionand return. c) 在内部函数中将计数器增加 100 并返回。 d)Call “Increment” three times and store the returned value in a variable each time. d) 调用“Increment” 3 次,每次将返回值存储在一个变量中。 e)Log the final value in the web console (400 is the final valuefor the third call), but function does not return even the first value. e) 在 web 控制台中记录最终值(400 是第三次调用的最终值),但 function 甚至不返回第一个值。

Can you guys see where the problem is?大家能看出问题出在哪里吗?

You should call the "inner" function for it to execute.您应该调用“内部” function 来执行它。

And your Increment function Should return or update counter variable for it to take effect.并且您的增量 function 应该返回或更新计数器变量以使其生效。

You should also declare the counter variable outside so you can increment it every time you call the "Increment" Function.您还应该在外部声明计数器变量,以便每次调用“增量”Function 时都可以递增它。

If you declare it inside it will just reset every time you call the "Increment" function.如果你在里面声明它,它只会在你每次调用“增量”function 时重置。

you can do it like this.你可以这样做。

function Increment()
{
    function inner() 
    {
        counter += 100;
        return counter;
    }
    return inner();
}

var counter = 100;

console.log(Increment());
console.log(Increment());
console.log(Increment());

You will not only have to call the inner function but also return its result.您不仅要调用内部 function 还要返回其结果。 The below code works下面的代码有效

 var counter = 100; function Increment() { function inner() { counter += counter; return counter; } return inner(); } var x = Increment(); var y = Increment(); console.log(x); console.log(y);

You need to call the inner function three times, so you need to return the inner function in the outer function and call it.内层function需要调用3次,所以需要在外层function中返回内层function并调用。

 function Increment() { var counter = 100; function inner() { counter += counter; return counter; } return inner; } var x = Increment(); console.log(x()); console.log(x()); console.log(x());

If the final value needs to be 400, you can rewrite your function a little bit like this:如果最终值需要为 400,您可以像这样重写您的 function:

 function Increment() { var counter = 0; function inner() { counter += counter || 100; return counter; } return inner; } var x = Increment(); console.log(x()); console.log(x()); console.log(x());

Increment() returns nothing, or undefined . Increment()不返回任何内容或undefined

If you expect Increment to return the value of counter, simply add a return statement.如果您希望 Increment 返回 counter 的值,只需添加一个return语句。

 function Increment() {
     let counter = 100;
     function inner() {
         counter += counter;
         return counter;
     }
     return counter;
}
let x = Increment();
console.log(x); // 100
console.log(x); // no change, since `Increment()` 
console.log(x); // was only called once

Keep in mind, inner is never called — if you wanted inner to do something, call inner() .记住, inner永远不会被调用——如果你想让 inner 做某事,调用inner() Additionally, keep in mind that counter is declared within Increment() , meaning that each time you call it, it is redeclared (and reset to 100)此外,请记住, counter是在Increment()中声明的,这意味着每次调用它时,都会重新声明它(并重置为 100)

Those instructions won't produce the result they anticipate.这些指令不会产生他们预期的结果。

Here's probably what they actually want.这可能是他们真正想要的。

 function Increment() { var counter = 100; function inner() { counter += 100; return counter; } return inner; } var innerFunc = Increment(); var a = innerFunc(); var b = innerFunc(); var c = innerFunc(); console.log(c);

Two things changed in Increment . Increment改变了两件事。

First, the inner function increments the counter by 100 .首先, inner function 将计数器增加100 This is needed because counter is mutated, so counter += counter causes counter to increase by the most recently updated version.这是必需的,因为counter发生了变化,因此counter += counter导致counter增加最近更新的版本。

Second, the inner function becomes the return value of Increment .其次, inner function 成为Increment的返回值。

So you'd only call Increment once, and store its result in a variable.因此,您只需调用一次Increment ,并将其结果存储在一个变量中。 That variable holds the inner function you returned.该变量包含您返回的inner function。 Now every time you call that function, it increments the counter variable by 100 , and returns its new value.现在每次调用 function 时,它都会将counter变量增加100并返回其新值。

This is a proper demonstration of closures.这是对闭包的正确演示。

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

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