开发者

Javascript object calling value with dynamic variable

This is driving me insane. I am trying to grab an object in javascript using a dynamic variable. Static variables appear to work, yet I cant for the life of me see the difference between the static vars I put in and the dynamic vars.

My snippet below better illustrates the issue

console.log(itemId); //E2
console.log(typeof itemId); //string
console.log(typeof 'E2'); //string
console.log(ganttObject.items['E2']); //obj开发者_开发百科ect [with data]
console.log(ganttObject.items[itemId]); //undefined

The weird thing is that I have extensively used dynamic calling of objects elsewhere in the script, but it is breaking here.

Tested in Safari & Chrome


Works for me in FF4:

Javascript object calling value with dynamic variable

Are you sure you don't have a typo or something in your actual code?


I found the answer at last by tracing the value of itemId to the source. Turns out an edit in the html generation added a space after my id.

Moral of the story:

Remember that trailing spaces won't show up on the console window

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜