Javascript 代码仅在未包装在函数中时有效

Javascript 代码仅在未包装在函数中时有效,javascript,Javascript,真的不知道这里发生了什么,因为我是JavaScript新手,但实际上无法理解,即使在阅读了其他类似本文的文章之后:。出于某种原因,当我的代码放在函数中时,它返回“undefined”,而不是true或false 如果我使用不带函数的代码,并在第一行定义vara,它工作正常: var a = "wjebh ghbui ayub"; var b = (a.split(' ').join('')).split('a'); // creates array var trueOrFalse = b.ma

真的不知道这里发生了什么,因为我是JavaScript新手,但实际上无法理解,即使在阅读了其他类似本文的文章之后:。出于某种原因,当我的代码放在函数中时,它返回“undefined”,而不是true或false

如果我使用不带函数的代码,并在第一行定义var
a
,它工作正常:

var a = "wjebh ghbui ayub";
var b = (a.split(' ').join('')).split('a'); // creates array

var trueOrFalse = b.map(function(c, i){ // puts into array true/false for each index
    if (c[2] == 'b') {
        console.log('value: ' + c[2] + ' is b; true');
        return true;
    } else {
        console.log('false');
        return false;
    }
});

var answer = function(el) {
    // checks whether any element is true
    return el === true;
};
trueOrFalse.some(answer); // return true/false
但当我将它添加到函数中时,它就不起作用了

function bThreeAfterA(a) {
    var b = (a.split(' ').join('')).split('a'); // creates array

    var trueOrFalse = b.map(function(c, i){ // puts into array true/false for each index
        if (c[2] == 'b') {
            console.log('value: ' + c[2] + ' is b; true');
            return true;
        } else {
            console.log('false');
            return false;
        }
    });

    var answer = function(el) {
 // checks whether any element is true
        return el === true;     
    };

    trueOrFalse.some(answer); // return true/false
}
即使它在生活中也不能正常工作:

(function(){
    var a = "wjebh ghbui ayub";
    var b = (a.split(' ').join('')).split('a'); // creates array

    var trueOrFalse = b.map(function(c, i){ // puts into array true/false for each index
        if (c[2] == 'b') {
            console.log('value: ' + c[2] + ' is b; true');
            return true;
        } else {
            console.log('false');
            return false;
        }
    });

    var answer = function(el) {
        // checks whether any element is true
        return el === true;
    };
    trueOrFalse.some(answer); // return true/false
})();
我觉得我在做一些很愚蠢的事情,大多数人很容易发现。有人能解释一下我做错了什么吗?这大概是某种初学者语法错误。任何阅读资源的链接也会很有帮助。

我们都去过那里

试试看

function bThreeAfterA(a) {
    var b = (a.split(' ').join('')).split('a'); // creates array

    var trueOrFalse = b.map(function(c, i){ // puts into array true/false for each index
        if (c[2] == 'b') {
            console.log('value: ' + c[2] + ' is b; true');
            return true;
        } else {
            console.log('false');
            return false;
        }
    });

    var answer = function(el) {
 // checks whether any element is true
        return el === true;     
    };

    return trueOrFalse.some(answer); //ACTUALLY return true/false
}

console.log("Answer: " + bThreeAfterA("wjebh ghbui ayub")); // Returns true. 
当您对块作用域之外的变量进行操作时,它们将保持不变。当他们在里面的时候,他们就消失了


因此,它“工作”了,但当你把它放在一个函数中时它没有“工作”。

你把这个js放在哪里?你的函数没有返回任何东西……定义“不工作”?发生了什么事?你预计会发生什么?我真的不知道你想做什么,但你的问题不是在
true或false之前缺少
return
。一些(答案)
?如果要在调用函数后检索值,必须
返回所需的值