[英]Why I'm getting an error when looking for an index using window variable?
This is very strange, I'm trying to use a variable which I define as is: 这很奇怪,我试图使用一个按原样定义的变量:
window.params['variable'] = { ... };
In the function I'm using I have this: 在我正在使用的函数中,我有这个:
function q()
{
...
// for example
return new Chart( canvas, window.params['variable'] );
...
}
When I execute this I get the error: Uncaught TypeError: Cannot read property 'variable' of undefined
当我执行此操作时,我得到一个错误:
Uncaught TypeError: Cannot read property 'variable' of undefined
But, incredible (for me), when I define params as: 但是,当我将参数定义为:
window.params = { ... }
The error disappear, and the function runs fine! 错误消失,功能正常运行! why?
为什么?
(Obviusly I replace the window.params['variable']
by window.params
first, but I believe that this is equivalent) (Obviusly我更换
window.params['variable']
通过window.params
第一,但我认为,这是等效)
Thanks. 谢谢。
You need to define params
first before adding properties to params
. 您需要定义
params
第一属性添加到之前params
。 So the right way to do it is 所以正确的方法是
window.params = {};
window.params['variable'] = { };
or 要么
window.params = {
'variable' : {}
};
and then 接着
function q(){
...
// for example
return new Chart( canvas, window.params['variable'] );
...
}
You will get more information about this here : https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Object_initializer 您将在这里获得有关此的更多信息: https : //developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Object_initializer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.