You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用unlockMaterial函数时数组参数materialCosts为何返回undefined?

Why does my materialCosts array parameter return undefined in the function call?

I'm calling the function unlockMaterial('Ore', 0, ['money', 100]) with the following definition:

function unlockMaterial (material, index, materialCosts) {
    console.log(material)
    console.log(index)
    console.log(materialCosts)
}

But the console outputs Ore 0 undefined. I can't figure out why the materialCosts array is coming through as undefined. For context, I plan to use a similar call pattern later for another function: craftItem(material, array).


Let's break down the possible reasons why your materialCosts parameter is showing up as undefined—your initial function definition and call look syntactically correct, so this is likely a hidden issue in your code:

1. Your function was redefined elsewhere with fewer parameters

It’s common to accidentally overwrite a function later in your code. If somewhere after the original unlockMaterial definition, you have another version that only accepts 2 parameters, that’s the one being executed:

// This later definition replaces the original
function unlockMaterial(material, index) {
  console.log(material);
  console.log(index);
  console.log(materialCosts); // materialCosts isn't a parameter here, so it's undefined
}

To verify this, log the function’s source code right before your call:

console.log(unlockMaterial.toString());

This will show you the actual function definition that’s being used.

2. A typo or hidden error in your function call

Double-check the line where you call unlockMaterial:

  • Did you accidentally omit the third parameter entirely? (e.g., unlockMaterial('Ore', 0) instead of including the array)
  • Is the array being passed as a variable that’s accidentally set to undefined? (e.g., const costs = undefined; unlockMaterial('Ore', 0, costs))
  • Did you use invalid syntax for the array? (Like Chinese brackets 【】 instead of square brackets []—though this would throw a syntax error, so your code wouldn’t run at all if this were the case)

3. The parameter is being shadowed inside the function

If you have a variable named materialCosts defined inside the function, it will overwrite the input parameter:

function unlockMaterial (material, index, materialCosts) {
  let materialCosts = undefined; // This shadows the input parameter
  console.log(material);
  console.log(index);
  console.log(materialCosts); // Logs the internal undefined variable, not the input
}

Scan the function body for any declarations of materialCosts that might be causing this conflict.

For your upcoming craftItem call

Keep these same checks in mind:

  • Confirm the craftItem function accepts the number of parameters you’re passing
  • Avoid redefining the function later in your code
  • Make sure no internal variables shadow the array parameter

内容的提问来源于stack exchange,提问作者bejewelled

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:55:44