Javascript - return语句未按预期工作

时间:2018-01-06 17:46:13

标签: javascript html return

我的功能有问题。我知道返回我想要的变量的行正在到达,但由于某种原因它没有返回。这是我的功能:

console.log(getLabel(mySelectElementNode)); // this is undefined

function getLabel(element) {
    // This function tries to find the label associated with the select element
    // select.labels is the usual property, but not all browsers support it

    //get the sibling nodes of the label we need
    var siblings = element.parentNode.parentNode.childNodes;
    siblings.forEach(function (elem,ind) {
        if (elem.nodeName=='LABEL') {
            console.log(elem); //I can see this in my browser and it is the label I am looking for
            return elem; // but this return statement does nothing
        };
    });
    console.log('label not found');
}

我不明白为什么我的退货声明不起作用。我也试过把return null;在console.log语句之后的函数末尾,但即使这样也没有用。

2 个答案:

答案 0 :(得分:0)

您是从forEach回调而不是getLabel功能返回的。这不只是你,很多人在某些时候犯了这个错误。 :-)相反,请确保return位于任何嵌套函数的getLabel 之外

在你的情况下,你不想要forEach,你想要find

function getLabel(element) {
    // This function tries to find the label associated with the select element
    // select.labels is the usual property, but not all browsers support it

    //get the sibling nodes of the label we need
    var siblings = element.parentNode.parentNode.childNodes;
    return Array.prototype.find.call(siblings, function(elem) {
        return elem.nodeName == "LABEL";
    });
}
在ES2015中添加了

find,但可以进行polyfilled;上面的链接有一个polyfill。

请注意childNodes不是数组,这就是我们上面使用Function#call的原因。

如果您想在没有填充物的情况下进行此操作,可以使用ES5(2009)中添加的some

function getLabel(element) {
    // This function tries to find the label associated with the select element
    // select.labels is the usual property, but not all browsers support it

    //get the sibling nodes of the label we need
    var siblings = element.parentNode.parentNode.childNodes;
    var result = undefined; // The default is `undefined`, of course; this is just for clarity
    Array.prototype.some.call(siblings, function(elem) {
        if (elem.nodeName == "LABEL") {
            result = elem;
            return true;
        }
    });
    return result;
}

some调用回调,直到它返回真值。

当然,还有一个简单的for循环:

function getLabel(element) {
    // This function tries to find the label associated with the select element
    // select.labels is the usual property, but not all browsers support it

    //get the sibling nodes of the label we need
    var sibling;
    for (sibling = element.parentNode.parentNode.firstElementChild; sibling; sibling = sibling.nextElementSibling) {
        if (sibling.nodeName == "LABEL") {
            return sibling;
        }
    }
    return undefined; // Again, just for emphasis/clarity
}

答案 1 :(得分:-1)

因为你从forEach的回调函数返回,这就是为什么它不能正常工作。因为您正在调用getLable,所以您必须在getLable函数的范围内编写return语句。但是你的return语句属于内部回调函数的范围。正确的代码如下。

console.log(getLabel(mySelectElementNode)); 
// this is undefined
 function getLabel(element) { 
// This function tries to find the label associated with the select element
 // select.labels is the usual property, but not all browsers support it
 //get the sibling nodes of the label we need
var newElem;
 var siblings = element.parentNode.parentNode.childNodes;
 siblings.forEach(function (elem,ind) {
 if (elem.nodeName=='LABEL') {
     newElem = elem;
console.log(elem); //I can see this in my browser and it is the label I am looking for 
return elem; // but this return statement does nothing
 }; }); console.log('label not found'); 
return newElem;
}