无法调用方法“split”;未定义的 - 从函数调用

发布于 2024-12-17 07:03:37 字数 1366 浏览 0 评论 0原文

我有一个在加载时调用的 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();
}

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

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:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-小熊_ 2024-12-24 07:03:37

在下面的内容中:

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

标识符 currentAttachments 的使用就像是一个全局变量一样。如果在调用该函数时尚未为其赋值,或者其值不是字符串,则会出现错误。

因此,修复方法是确保它具有字符串值:

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

或以其他方式处理错误。

另外,当您执行所有这些 if..else 块时,请考虑:

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

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

In the following:

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

The identifier currentAttachments is 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.

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:

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.

相思故 2024-12-24 07:03:37

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

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

首先,显式传递第一个函数中的附件:

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

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

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

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

如果您更加小心地管理作用域并正确定义函数来接收所需的参数并对其进行操作,那么您将来会避免很多头痛,并且会大大减少此类问题。

You are misunderstanding/misusing the scope rules of 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 currentAttachments argument to the function rather than just relying on a previously-declared global variable. Second, I'm declaring myAttachmentArray as a local variable by using the var keyword. Declaring variables with var declares them in local scope; failing to do so declares them in global scope. Third, I'm manually passing the array to the setupWeaponAttachments function, in which I will also receive the argument:

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

Notice that I again properly declare the myAttachmentForWeapon variable in local scope.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文