简体   繁体   English

为什么写一个永远不会返回值的函数

[英]why write a function that never returns a value

I am reading a very comprehensive javascript programming book. 我正在阅读一本非常全面的javascript编程书。 In the "functions" section of language basics I have come to the following: 在语言基础的“功能”部分,我得到了以下内容:

"The return statement can also be used without specifying a return value. When used in this way, the function stops executing immediately and returns undefined as its value. This is typically used in functions that don't return a value to stop function execution early, as in the following example, where the alert won't be displayed:" “也可以在不指定返回值的情况下使用return语句。当以这种方式使用时,函数会立即停止执行并返回undefined作为其值。这通常用于不返回值的函数以提前停止函数执行,如下例所示,警报不会显示:“

function sayHi(name, message) {
return;
alert(“Hello “ + name + “, “ + message); //never called
}

I am trying to understand why anyone would want to do this. 我试图理解为什么有人想要这样做。 Why write a function that returns "undefined"? 为什么要写一个返回“undefined”的函数? I have tried googling, and searching SO, but have not had much luck though this may be because I am not phrasing my search correctly as I am learning js. 我试过谷歌搜索,搜索SO,但没有太多的运气虽然这可能是因为我没有正确地描述我的搜索,因为我正在学习js。

Can anyone give me a real-world example of where this might be useful so that I can understand? 任何人都可以给我一个真实世界的例子,说明这可能有用,这样我才能理解?

Usually it's a conditional return. 通常这是一个有条件的回报。 Something like 就像是

function calculateSomething(obj, condition) {
   if (condition !=0 ) return;

   obj.data = obj.data * 42;
}

in this case if some condition fails - function exits right away. 在这种情况下,如果某些条件失败 - 函数立即退出。 Otherwise data in a passed object is modified. 否则,将修改传递的对象中的数据。

The function you provide is useless... 你提供的功能是无用的......

However, not every function needs to return something. 但是,并非每个功能都需要返回一些东西。 Sometimes a function does things "elsewhere" and the value that is returned is irrelevant. 有时函数会在“其他地方”执行操作,并且返回的值无关紧要。

The function may have side effects, such as altering the state of the page. 该功能可能有副作用,例如改变页面状态。 Then, the caller may not expect any return value. 然后,调用者可能不期望任何返回值。

In my opinion, learned from masters so to speak, the idea here is to only temporarily disable the function. 在我看来,从大师那里学到了,这里的想法是暂时禁用该功能。 So if you have like an x amount of function calls to sayHi() like for example in a repeater or spread out over multiple files, the idea can be useful to simply return; 因此,如果你喜欢对sayHi()进行x次函数调用,例如在转发器中或者分散在多个文件上,那么这个想法对于简单return;是有用的return; without a value. 没有价值。 Useful? 有用? Yes, to avoid commenting out chunks of code, but that's it. 是的,为了避免注释掉代码块,但就是这样。

Leaving this in a development environment, which initially will enter the www , you should not write it like this, and always make sure that: when a function has to return "something", then "something" always counts for value, no matter what the condition. 将此保留在最初将进入www的开发环境中, 你不应该这样写,并且总是要确保:当一个函数必须返回“某事”时,那么“某事”总是值得重视,无论什么条件。 With lowest value a boolean => return false; 使用最小值boolean => return false; . This counts for every code block inside that function => for if else while ... 这计算该函数内的每个代码块=> for if else while ...

/* INCORRECT: it only returns something conditionally */
function do(something){
    if (condition){
        return false;
    }
}

/* CORRECT: it returns value no matter what the condition */
function do(something){
    if (!condition){
        return true;
    }

    return false;
}

/* OPTIMIZED: a lot of ideas can be optimized, especially when returning booleans */
function do(something){
    return !condition;
}

The rules only apply if you want to write proper code. 仅当您要编写正确的代码时,规则才适用。 Using tools like jslint, jshint or re-sharper helps a lot in understanding the basic principles of writing ecmascript valid code. 使用jslint,jshint或re-sharper等工具有助于理解编写ecmascript有效代码的基本原则。 Since not everyone is aware of these rules, all examples will yield the same result. 由于并非每个人都了解这些规则,因此所有示例都会产生相同的结果。 In the sens of "it works, but it's not valid code". 在“它工作,但它不是有效的代码”的意义上。

Because A JS function is an object(has:Attributs+methods) 因为A JS函数是一个对象(具有:Attributs +方法)

And , An object should have a value which can be undefined . 并且,对象应具有可以未定义的值。

The proof is that 证明是这样的

function sayHi(){}

Can be handled such as object even calling it : 可以像对象一样处理甚至调用它:

var returnedValue=sayHi.call() ;

If sayHi does not return anything, returnedValue will be undefined 如果sayHi没有返回任何内容,则returnedValue将是undefined

Example if you want to use classes I am sure about it you will use setter functions to give value for an instance attribute. 示例如果要使用类我确定它将使用setter函数为实例属性赋值。

Like this: 像这样:

function Something(val) {
    this.value = val;
}

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

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