我应该总是给我的功能一个返回值吗?

时间:2022-09-10 23:19:05

I write JavaScript code and I try to use its functional language nature.

我编写JavaScript代码,并尝试使用其功能语言本质。

In other functional languages (or even in Ruby), if I don't explicitly set the return value of a function, it will return the value of the last evaluated expression. JavaScript does not follow this pattern. (To be precise, JavaScript always returns a value as well. If nothing was set, then undefined.)

在其他函数语言中(甚至在Ruby中),如果我没有显式设置函数的返回值,它将返回最后一个计算表达式的值。 JavaScript不遵循这种模式。 (确切地说,JavaScript总是返回一个值。如果没有设置,那么未定义。)

My question is the following: I have a function that doesn't need to (and does not) return a value. Does it make sense in a functional programming context to have a function with no explicit return value? Or did a fail somewhere if I found myself in this case?

我的问题如下:我有一个不需要(也没有)返回值的函数。在函数式编程上下文中,有一个没有显式返回值的函数是否有意义?如果我发现自己处于这种情况下,或者在某处失败了?

For example, I have the following function. It checks periodically if location hash was changed, and if so, calls the given function.

例如,我有以下功能。它会定期检查位置散列是否已更改,如果是,则调用给定的函数。

LIB_hashManager = function(f, context) {
    var prev = '';
    var pollHash = function() {
        if (prev !== window.location.hash) {
            prev = window.location.hash;
            f.apply(context);
        }
    };
    window.setInterval(pollHash, 100);
};

Should I return here anything?

我应该回到这里吗?

Update

Meanwhile it came to my mind, that if anytime in the future I'll need to extend the knowledge of LIB_hashManager, following the functional constructor pattern, I can simply add methods to an object and LIB_hashManager will return that produced object.

同时我想到,如果将来任何时候我需要扩展LIB_hashManager的知识,遵循功能构造函数模式,我可以简单地向对象添加方法,LIB_hashManager将返回该生成的对象。

LIB_hashManager = function(f, context) {
    // inside logic
    // ...
};

And later I may write:

后来我写道:

LIB_hashManager = function(f, context) {
    // inside logic
    // ...

    // return public methods
    return {
        // ...
    }
};

So doesn't make sense then to return an empty object in the first case?

那么在第一种情况下返回空对象没有意义吗?

6 个解决方案

#1


A "pure" functional programming environment would have no side effects -- each function's work would be entirely in computing its return value; that's not really feasible in typical uses of Javascript, and so it's perfectly acceptable, when a function has done its work through side effects, to have it return nothing at all, i.e., be a "procedure" rather than a function.

“纯粹的”函数编程环境没有任何副作用 - 每个函数的工作完全在于计算其返回值;这在Javascript的典型使用中并不是真的可行,因此当一个函数通过副作用完成它的工作时,它完全可以接受,它根本不返回任何内容,即,是一个“过程”而不是一个函数。

#2


My question is the following: I have a function that doesn't need to (and does not) return a value. Does it make sense in a functional programming context to have a function with no explicit return value? Or did a fail somewhere if I found myself in this case?

我的问题如下:我有一个不需要(也没有)返回值的函数。在函数式编程上下文中,有一个没有显式返回值的函数是否有意义?如果我发现自己处于这种情况下,或者在某处失败了?

According to the academic description of a function: a function must give the same output given an input. A function that does not have any output is absolutely useless, because functions are not supposed to have any side effects.

根据函数的学术描述:函数必须在给定输入的情况下给出相同的输出。没有任何输出的函数绝对没用,因为函数不应该有任何副作用。

However, since functional programming languages often need at least 1 side effect, the convention to return nothing is to return a unit or "()". Since this concept does not exist in Javascript, it shouldn't matter to you, since Javascript isn't strongly typed anyway.

但是,由于函数式编程语言通常需要至少1个副作用,因此返回任何内容的约定是返回单元或“()”。由于这个概念在Javascript中不存在,因此对你来说无关紧要,因为无论如何Javascript都不是强类型的。

#3


It is perfectly fine to have a function that does not return anything. In fact, forcing a function that naturally would not have a return value to have one is awkward and smells bad.

拥有一个不返回任何东西的功能是完全没问题的。事实上,强制一个自然没有返回值的函数有一个是笨拙和闻起来很糟糕。

#4


The function which produces only side effect may be considered simply as an isolated program block, same as procedure block. Since JS doesn't have procedures, there is nothing wrong to use a function as a procedure block. The only exception that functions in JS are objects too, so be careful with extensive use of such 'functions'.

仅产生副作用的函数可以简单地视为孤立的程序块,与过程块相同。由于JS没有过程,因此将函数用作过程块没有任何错误。 JS中函数的唯一例外也是对象,所以要小心大量使用这些“函数”。

In this case it just increases readability of the program.

在这种情况下,它只是增加了程序的可读性。

#5


If you are not supposed to use the result of LIB_hashManager I think you should definetly return undefined (i.e., have no return statement at all).

如果你不应该使用LIB_hashManager的结果,我认为你应该definetly返回undefined(即根本没有return语句)。

If you forget this and try to use the result of the function anyway you'll probably just get an error (which is fine since that would be a programming error, a bug!)

如果你忘记了这一点,并尝试使用函数的结果,你可能只会得到一个错误(这很好,因为这将是一个编程错误,一个错误!)

#6


This question has been answered over 4 years ago, but I believe, the accepted answer is wrong

这个问题已在4年前得到解答,但我相信,接受的答案是错误的


In the code given, author sets interval and then - he does not provide ANY way to stop it.

在给出的代码中,作者设置间隔然后 - 他没有提供任何方法来阻止它。

And so the answer should be: Yes, you should return a value from this function, and that value should be an object allowing you to stop interval, which is started inside of it.

所以答案应该是:是的,你应该从这个函数中返回一个值,该值应该是一个允许你停止在它内部启动的间隔的对象。

Discussing details of how this should be implemented is out of scope. // you can do it either by returning interval handler, so you can cancel it manually ( see example 1) or by returning an object with method that does that behind the scene (smth like .pause, .stop or .cancel). Alternatively, that object can also allow run-time reconfiguration of hash manager (like changing the interval frequency).

讨论如何实施这一点的细节超出了范围。 //你可以通过返回间隔处理程序来做到这一点,这样你就可以手动取消它(参见例1),或者通过返回一个方法在场景后面执行该操作(如.pause,.stop或.cancel)。或者,该对象还可以允许哈希管理器的运行时重新配置(如更改间隔频率)。

Example 1 (simple):

例1(简单):

LIB_hashManager = function(f, context) {
    var prev = '';
    var pollHash = function() {
        if (prev !== window.location.hash) {
            prev = window.location.hash;
            f.apply(context);
        }
    };
    return window.setInterval(pollHash, 100);
};

#1


A "pure" functional programming environment would have no side effects -- each function's work would be entirely in computing its return value; that's not really feasible in typical uses of Javascript, and so it's perfectly acceptable, when a function has done its work through side effects, to have it return nothing at all, i.e., be a "procedure" rather than a function.

“纯粹的”函数编程环境没有任何副作用 - 每个函数的工作完全在于计算其返回值;这在Javascript的典型使用中并不是真的可行,因此当一个函数通过副作用完成它的工作时,它完全可以接受,它根本不返回任何内容,即,是一个“过程”而不是一个函数。

#2


My question is the following: I have a function that doesn't need to (and does not) return a value. Does it make sense in a functional programming context to have a function with no explicit return value? Or did a fail somewhere if I found myself in this case?

我的问题如下:我有一个不需要(也没有)返回值的函数。在函数式编程上下文中,有一个没有显式返回值的函数是否有意义?如果我发现自己处于这种情况下,或者在某处失败了?

According to the academic description of a function: a function must give the same output given an input. A function that does not have any output is absolutely useless, because functions are not supposed to have any side effects.

根据函数的学术描述:函数必须在给定输入的情况下给出相同的输出。没有任何输出的函数绝对没用,因为函数不应该有任何副作用。

However, since functional programming languages often need at least 1 side effect, the convention to return nothing is to return a unit or "()". Since this concept does not exist in Javascript, it shouldn't matter to you, since Javascript isn't strongly typed anyway.

但是,由于函数式编程语言通常需要至少1个副作用,因此返回任何内容的约定是返回单元或“()”。由于这个概念在Javascript中不存在,因此对你来说无关紧要,因为无论如何Javascript都不是强类型的。

#3


It is perfectly fine to have a function that does not return anything. In fact, forcing a function that naturally would not have a return value to have one is awkward and smells bad.

拥有一个不返回任何东西的功能是完全没问题的。事实上,强制一个自然没有返回值的函数有一个是笨拙和闻起来很糟糕。

#4


The function which produces only side effect may be considered simply as an isolated program block, same as procedure block. Since JS doesn't have procedures, there is nothing wrong to use a function as a procedure block. The only exception that functions in JS are objects too, so be careful with extensive use of such 'functions'.

仅产生副作用的函数可以简单地视为孤立的程序块,与过程块相同。由于JS没有过程,因此将函数用作过程块没有任何错误。 JS中函数的唯一例外也是对象,所以要小心大量使用这些“函数”。

In this case it just increases readability of the program.

在这种情况下,它只是增加了程序的可读性。

#5


If you are not supposed to use the result of LIB_hashManager I think you should definetly return undefined (i.e., have no return statement at all).

如果你不应该使用LIB_hashManager的结果,我认为你应该definetly返回undefined(即根本没有return语句)。

If you forget this and try to use the result of the function anyway you'll probably just get an error (which is fine since that would be a programming error, a bug!)

如果你忘记了这一点,并尝试使用函数的结果,你可能只会得到一个错误(这很好,因为这将是一个编程错误,一个错误!)

#6


This question has been answered over 4 years ago, but I believe, the accepted answer is wrong

这个问题已在4年前得到解答,但我相信,接受的答案是错误的


In the code given, author sets interval and then - he does not provide ANY way to stop it.

在给出的代码中,作者设置间隔然后 - 他没有提供任何方法来阻止它。

And so the answer should be: Yes, you should return a value from this function, and that value should be an object allowing you to stop interval, which is started inside of it.

所以答案应该是:是的,你应该从这个函数中返回一个值,该值应该是一个允许你停止在它内部启动的间隔的对象。

Discussing details of how this should be implemented is out of scope. // you can do it either by returning interval handler, so you can cancel it manually ( see example 1) or by returning an object with method that does that behind the scene (smth like .pause, .stop or .cancel). Alternatively, that object can also allow run-time reconfiguration of hash manager (like changing the interval frequency).

讨论如何实施这一点的细节超出了范围。 //你可以通过返回间隔处理程序来做到这一点,这样你就可以手动取消它(参见例1),或者通过返回一个方法在场景后面执行该操作(如.pause,.stop或.cancel)。或者,该对象还可以允许哈希管理器的运行时重新配置(如更改间隔频率)。

Example 1 (simple):

例1(简单):

LIB_hashManager = function(f, context) {
    var prev = '';
    var pollHash = function() {
        if (prev !== window.location.hash) {
            prev = window.location.hash;
            f.apply(context);
        }
    };
    return window.setInterval(pollHash, 100);
};