“元素列表后缺少]”与评估
我试图将两个参数传递给函数,“i”是一个 int 值,“map”是一个对象。我使用 eval 通知 Javascript 我正在传递对象,但是我收到错误“元素列表后缺少 ]”。
我对此有点陌生,所以我不能 100% 确定这是否是正确的做事方式……但是是的……希望我走在正确的轨道上。
...
optionsControlDiv.innerHTML += '<input type="checkbox" onclick="toggleLayer('+eval(i)+","+eval(map)+')"'+c+' />'+this.opts[i]+'<br>';
console.log('<input type="checkbox" onclick="toggleLayer('+eval(i)+","+eval(map)+')"'+c+' />'+this.opts[i]+'<br>');
// OUTPUT:
// <input type="checkbox" onclick="toggleLayer(0,[object Object])" />Wiki<br>
// <input type="checkbox" onclick="toggleLayer(1,[object Object])" />webcams<br>
// <input type="checkbox" onclick="toggleLayer(2,[object Object])" />Photos<br>
// ERROR:
// missing ] after element list
// [Break on this error] toggleLayer(0,[object Object])
...
// The function
function toggleLayer(i,map) {
//console.log(i);
if (layers[i].Visible) {
layers[i].hide();
} else {
if(layers[i].Added) {
layers[i].show();
} else {
map.addOverlay(layers[i]);
layers[i].Added = true;
}
}
layers[i].Visible = !layers[i].Visible;
}
im trying to pass two parameters to a function, "i" being an int value and "map" being an object. I'm using eval to inform Javascript that I am passing objects, however im getting the error "missing ] after element list".
I'm kinda new to this so I'm not 100% sure if this is the right way of doing things... but ya.. hopefully i'm on the right track.
...
optionsControlDiv.innerHTML += '<input type="checkbox" onclick="toggleLayer('+eval(i)+","+eval(map)+')"'+c+' />'+this.opts[i]+'<br>';
console.log('<input type="checkbox" onclick="toggleLayer('+eval(i)+","+eval(map)+')"'+c+' />'+this.opts[i]+'<br>');
// OUTPUT:
// <input type="checkbox" onclick="toggleLayer(0,[object Object])" />Wiki<br>
// <input type="checkbox" onclick="toggleLayer(1,[object Object])" />webcams<br>
// <input type="checkbox" onclick="toggleLayer(2,[object Object])" />Photos<br>
// ERROR:
// missing ] after element list
// [Break on this error] toggleLayer(0,[object Object])
...
// The function
function toggleLayer(i,map) {
//console.log(i);
if (layers[i].Visible) {
layers[i].hide();
} else {
if(layers[i].Added) {
layers[i].show();
} else {
map.addOverlay(layers[i]);
layers[i].Added = true;
}
}
layers[i].Visible = !layers[i].Visible;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
map
是全局变量,则无需使用 eval 即可工作:您会收到错误,因为
eval
需要字符串作为参数,而map
对象则为作为字符串调用返回正在评估并导致错误的[object Object]
。If
map
is global variable this should work without using eval:You've got an error because
eval
expects string as parameter, andmap
object while called as string returns[object Object]
which is being evaluated and results in error.