英文:
Why does this function return a number and not another function?
问题
抱歉,我理解了您的请求。以下是您提供的代码的翻译部分:
抱歉,问题表达得不够清晰。我在尝试理解如何在JavaScript中在不同的函数调用之间保持状态时遇到了这段代码,但我不理解它的行为。它似乎返回的是一个函数,而不是一个数字。那么为什么 typeof(f()) == 'number'
为真呢?
var f = function() {
var state = 1;
return function() {
return state++;
};
}();
console.log(typeof f());
请注意,我已经删除了代码部分,只提供了翻译的内容。如果您有任何其他问题或需要进一步解释,请随时提出。
英文:
Sorry for the poorly phrased question. I came across this code when trying to understand how to maintain state across different function calls in javascript, and I don't understand its behaviour. It seems like it is returning a function, not a number. Why is typeof(f()) == 'number'
true then?
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var f = function() {
var state = 1;
return function() {
return state++;
};
}();
console.log(typeof f());
<!-- end snippet -->
答案1
得分: 0
你的代码返回了内部函数,因为你在代码的末尾添加了 (),然后调用了返回的函数并检查了返回值的类型,这个返回值是一个数字。
console.log(typeof f) // 函数
console.log(typeof f()) // 数字
英文:
your code is returning the inner function as you added () at the end of your code and you are calling that returned function and checking the type of that returned value which is a number.
console.log(typeof f) // function
console.log(typeof f()) // number
答案2
得分: 0
因为你在初始化 f
时调用了该函数,当你调用 f
时,基本上你调用的是外部函数返回的 function
。让我们看看在初始化的时候去掉括号:
var f = function() {
var state = 1;
return function() {
return state++;
};
};
console.log(typeof f());
如你所见,这是一个函数。现在,让我们加上你的代码中的 ()
,现在它是一个数字:
var f = function() {
var state = 1;
return function() {
return state++;
};
}();
console.log(typeof f());
因此,基本上你定义了一个函数并对其进行评估,将其返回的函数赋值给了 f
,所以 f
的结果的类型是一个数字。
英文:
Because you called the function when you initialized f
and, when you call f
, basically you call the function
that was returned by the outer function
. Let's see without the paranthesis at the end of the initialization:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var f = function() {
var state = 1;
return function() {
return state++;
};
};
console.log(typeof f());
<!-- end snippet -->
As you can see, it's a function then. Now, let's add the ()
your code has and now it's a number:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var f = function() {
var state = 1;
return function() {
return state++;
};
}();
console.log(typeof f());
<!-- end snippet -->
So, basically you define a function and evaluate it, assigning the function it returns to f
, so the type of f
's result is a number.
答案3
得分: -1
其行为很简单。f是一个函数,但当你说f()时,它实际上调用了该函数,并且该函数返回一个数字。
typeof(f) ---> 函数
typeof(f()) ---> typeof(f函数的返回值) - 这是一个数字
这就是为什么typeof(f()) === number。
英文:
Its behaviour is simple. f is a function, but when you say f() it will actually calls the function and the function returns a number.
typeof(f) ---> function
typeof(f()) ---> typeof(return value of f function) - which is a number
Thats why typeof(f()) === number
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论