javascript 无法调用未定义的方法“拆分” - 从函数调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8206240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:38:52  来源:igfitidea点击:

Cannot call method 'split' of undefined - Called From Function

javascriptarraysvariablessplit

提问by Oliver Jones

I have a JS function that is called on load that spilts some variables, this all works well, but when I call the function from another function, I get this error Cannot call method 'split' of undefined:

我有一个在加载时调用的 JS 函数,它会拆分一些变量,这一切都很好,但是当我从另一个函数调用该函数时,出现此错误Cannot call method 'split' of undefined

function loadInAttachmentsIntoSquads(){
    // eg: 5000,5000,5000,5000 > [5000][5000][5000]
    myAttachmentArray = currentAttachments.split(',');

    //eg: [5000][5000][5000] > [5][0][0][0]
    //myAttachmentForWeapon = myAttachmentArray[mySquadsIndex].split('');

    setupWeaponAttachments();
}


function setupWeaponAttachments(){

    myAttachmentForWeapon = myAttachmentArray[mySquadsIndex].split('');

    //if(mySquadsIndex == 0){
        if(myAttachmentForWeapon[1] == 1){ // if silencer is on? //first digit is always 5
            weaponAttachments.silencer = true;
        }
        else{
            weaponAttachments.silencer = false;
        }
        if(myAttachmentForWeapon[2] == 1){ // if silencer is on? //first digit is always 5
            weaponAttachments.grip = true;
        }
        else{
            weaponAttachments.grip = false;
        }
        if(myAttachmentForWeapon[3] == 1){ // if silencer is on? //first digit is always 5
            weaponAttachments.redDot = true;
        }
        else{
            weaponAttachments.redDot = false;
        }

    // -- applies visuals -- \
    applyWeaponAttachments();
}

If I call setupWeaponAttachments()from another function, I get that error ... why?

如果我setupWeaponAttachments()从另一个函数调用,我会得到那个错误......为什么?

回答by RobG

In the following:

在下面的:

> function loadInAttachmentsIntoSquads(){
>     
>     myAttachmentArray = currentAttachments.split(',');
> 
>     setupWeaponAttachments(); 
> }

The identifier currentAttachmentsis used as if it's a global variable. If it hasn't been assigned value, or its value isn't a string, at the time that the function is called, then an error will result.

标识符currentAttachments用作全局变量。如果它没有被赋值,或者它的值不是一个字符串,那么在调用函数时,就会产生一个错误。

So the fix is to make sure it has a string value:

所以修复是确保它有一个字符串值:

function loadInAttachmentsIntoSquads(){
    if (typeof currentAttachments != 'string') return;
    ...
}

or deal with the error some other way.

或以其他方式处理错误。

Also, where you are doing all those if..else blocks, consider:

此外,在您执行所有 if..else 块的地方,请考虑:

weaponAttachments.silencer = myAttachmentForWeapon[1] == 1;
weaponAttachments.grip     = myAttachmentForWeapon[2] == 1;
weaponAttachments.redDot   = myAttachmentForWeapon[3] == 1;

It won't be any faster, but it is a lot less code to write and read.

它不会更快,但编写和读取的代码要少得多。

回答by Luke Sneeringer

You are misunderstanding/misusing the scope rules of JavaScript.

您误解/误用了 JavaScript 的范围规则。

Try passing the array you're splitting explicitly and consistently, and it should solve your problem, as well as keeping the global namespace less cluttered:

尝试明确且一致地传递您正在拆分的数组,它应该可以解决您的问题,并保持全局命名空间不那么混乱:

First, pass the attachments in your first function explicitly:

首先,在您的第一个函数中明确传递附件:

function loadInAttachmentsIntoSquads(currentAttachments) {
    var myAttachmentArray = currentAttachments.split(',');
    setupWeaponAttachments(myAttachmentArray);
}

Note several things I'm doing above. First, I'm adding a currentAttachmentsargumentto the function rather than just relying on a previously-declared global variable. Second, I'm declaring myAttachmentArrayas a local variable by using the varkeyword. Declaring variables with vardeclares them in local scope; failing to do so declares them in global scope. Third, I'm manually passing the array to the setupWeaponAttachmentsfunction, in which I will also receive the argument:

请注意我在上面做的几件事。首先,我向函数添加了一个currentAttachments参数,而不是仅仅依赖于先前声明的全局变量。其次,我myAttachmentArray使用var关键字声明为局部变量。声明变量 withvar在局部范围内声明它们;如果不这样做,则会在全局范围内声明它们。第三,我手动将数组传递给setupWeaponAttachments函数,我还将在其中接收参数:

function setupWeaponAttachments(myAttachmentArray) {
    var myAttachmentForWeapon = myAttachmentArray[mySquadsIndex].split('');
    // [...]
}

Notice that I again properly declare the myAttachmentForWeaponvariable in local scope.

请注意,我再次myAttachmentForWeapon在本地范围内正确声明了变量。

If you are more careful with managing scope and properly define functions to receive the arguments they need and operate on them, you'll save yourself lots of headache in the future, and you'll get drastically fewer problems like these.

如果您更小心地管理范围并正确定义函数以接收它们需要的参数并对其进行操作,那么您将来会省去很多麻烦,并且会大大减少此类问题。