无法调用方法“split”;未定义的 - 从函数调用
我有一个在加载时调用的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在下面的内容中:
标识符 currentAttachments 的使用就像是一个全局变量一样。如果在调用该函数时尚未为其赋值,或者其值不是字符串,则会出现错误。
因此,修复方法是确保它具有字符串值:
或以其他方式处理错误。
另外,当您执行所有这些 if..else 块时,请考虑:
它不会更快,但编写和读取的代码要少得多。
In the following:
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:
or deal with the error some other way.
Also, where you are doing all those if..else blocks, consider:
It won't be any faster, but it is a lot less code to write and read.
您误解/误用了 JavaScript 的范围规则。
尝试显式且一致地传递要拆分的数组,它应该可以解决您的问题,并保持全局命名空间不那么混乱:
首先,显式传递第一个函数中的附件:
请注意我上面所做的几件事。首先,我向函数添加一个
currentAttachments
参数,而不是仅仅依赖于先前声明的全局变量。其次,我使用var
关键字将myAttachmentArray
声明为局部变量。使用var
声明变量会在局部范围内声明它们;如果不这样做,就会在全局范围内声明它们。第三,我手动将数组传递给setupWeaponAttachments
函数,在该函数中我还将收到参数:请注意,我再次在本地范围内正确声明了
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:
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 declaringmyAttachmentArray
as a local variable by using thevar
keyword. Declaring variables withvar
declares them in local scope; failing to do so declares them in global scope. Third, I'm manually passing the array to thesetupWeaponAttachments
function, in which I will also receive the argument: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.