简体   繁体   English

无法通过JavaScript访问变量-范围错误?

[英]Cannot access variable through JavaScript - scope error?

I have some data in a separate .js file similar to this: 我在一个单独的.js文件中有一些类似于此的数据:

data = new Object();
data['cat'] = ['Mr. Whiskers','Wobbles'];
data['dog'] = ['Toothy'];
data['fish'] = ['goldy','roose'];

function getStuff(info)
{
  var stuff = data[info.value];
  return stuff;
}

Now in another html file with a block, I have something like this: 现在在另一个带有块的html文件中,我有类似以下内容:

function theDrop(dynamic) {
  alert(getStuff(dynamic));
}

The box says undefined , why? 框上显示undefined ,为什么?

What are you passing to theDrop ? 您要传递给theDrop什么? If you want to call the .value then you need to pass the whole object over otherwise you will get undefined 如果要调用.value则需要传递整个对象,否则将无法undefined

Live Demo 现场演示

var select = document.getElementById("selectme");

select.onchange = function(){
    theDrop(this);
}

data = new Object();
data['cat'] = ['Mr. Whiskers','Wobbles'];
data['dog'] = ['Toothy'];
data['fish'] = ['goldy','roose'];

function getStuff(info)
{
    var stuff = data[info.value];
    return stuff;
}

function theDrop(dynamic) {
    alert(getStuff(dynamic));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM