简体   繁体   English

JavaScript中的Return语句不会退出该功能?

[英]Return statement in javascript doesn't exit the function?

I wrote a simple function to sort objects not expecting it to work but it does: 我编写了一个简单的函数来sort不希望其起作用的对象进行sort ,但它确实可以:

function sortObjs(objArr,field) {
    objArr.sort(
        function(a,b) {
            if( isNaN(a[field]) ) {
                return a[field].localeCompare(b[field]);
            } else {
                return parseFloat(a[field]) - parseFloat(b[field])
            }
        }
    );

    return objArr;
}

When I call this function I get my sorted objects without issue. 当我调用此函数时,我得到的对象没有问题。 But I was not expecting it to work because I thought the first two return statements would exit the function before it got to the last statement: return objArr . 但是我并不期望它起作用,因为我认为前两个return语句会在到达最后一条语句之前返回该函数: return objArr

You have a nested function. 您有一个嵌套函数。 The first two returns will exit the inner function, while the last one will exit the outer one. 前两个返回将退出内部函数,而最后一个将退出外部函数。

EDIT: 编辑:

You can think of function returns as "replacing" the function with the returned value. 您可以认为函数返回就是用返回值“替换”函数。 For example: 例如:

var i = getSum(1, 3);

function getSum(a, b) {
    return a + b;
}

The function getSum returns the sum of a and b. 函数getSum返回a和b的总和。 The line var i = getSum(1, 3) will execute the lines of code contained in the function with a = 1 and b = 3 . var i = getSum(1, 3)将执行a = 1b = 3的函数中包含的代码行。 The value that is returned "replaces" the function call. 返回的值“替换”函数调用。 So now the line of code looks like var i = 4; 所以现在代码行看起来像var i = 4; . Although this is not exactly how it works, it's a good way to conceptualize it. 尽管这并不是确切的工作方式,但这是概念化的好方法。 This is a special case because you aren't actually running the inner method here, you're passing it as a variable. 这是一种特殊情况,因为您实际上没有在这里运行inner方法,而是将其作为变量传递。

Let me know if you have any more questions! 让我知道您是否还有其他问题!

To understand why the inner function's return statements would not have an effect on the return statement in the outer scope, you need to understand how the Array.prototype.sort() function works. 若要了解内部函数的return语句为何对外部作用域中的return语句无效的原因,您需要了解Array.prototype.sort()函数的工作方式。

The function arr.sort([compareFunction]) , takes a function as a parameter. 函数arr.sort([compareFunction]) ,将一个函数作为参数。

compareFunction Optional. compareFunction可选。 Specifies a function that defines the sort order. 指定定义排序顺序的函数。 If omitted, the array is sorted according to each character's Unicode code point value, according to the string conversion of each element. 如果省略,则根据每个字符的Unicode代码点值,以及每个元素的字符串转换,对数组进行排序。

  • The logic you write inside the compareFunction gets executed when the java script engine tries to compare two elements during its comparison operation. 当Java脚本引擎在比较操作期间尝试比较两个元素时,将执行您在compareFunction编写的逻辑。
  • Hence for each comparison that it makes, the function would return a value, based on which the elements would be ordered. 因此,对于每次进行的比较,该函数将返回一个值,基于该值将对元素进行排序。
  • This implies that the compareFunction that we pass on as a parameter would be used to just obtain a value based on which two elements can be compared and not exit the sort operation, leave alone exiting the outer function. 这意味着我们将作为参数传递的compareFunction将仅用于获取一个值,基于该值可以比较两个元素并且不退出排序操作,而仅退出外部函数。

Having said this, the code return objArr; 话虽如此,代码return objArr; , takes no effect, since the array would be sorted in place . ,由于该数组将在适当位置排序因此无效。

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

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