[英]javascript: public array with object
(sorry for my english=/) I create an array when 'onload'. (对不起,我的英语= /)我在“加载”时创建了一个数组。
(this: (这个:
var $ = {
ratio: 0.7,
screen: 500,
margin: 10,
canvas: document.getElementById("canvas"),
ctx: document.getElementById("canvas").getContext("2d"),
}
and $.canvas
and $.ctx
is give 'null'. 而
$.canvas
和$.ctx
是'null'。 this work if i rewrite it 如果我重写它的这项工作
...
canvas: function(){return document.getElementById("canvas")}
...
but if i want to call this the variable looks like this $.canvas () how can i make this variable without '()'??? 但是,如果我想调用此变量,则该变量看起来像$ .canvas ()一样,如何在没有'()'的情况下创建此变量呢?
Try this: 尝试这个:
window.onload = function(){
window.$ = {
ratio: 0.7,
screen: 500,
margin: 10,
canvas: document.getElementById("canvas"),
ctx: document.getElementById("canvas").getContext("2d"),
}
}
Looks like it's a problem with the scope of your $
variable. 看来您的
$
变数的范围有问题。 By declaring it with the var
keyword inside the window.onload
function, its scope is the local function and not the global scope. 通过在
window.onload
函数内部使用var
关键字进行声明,它的作用域是局部函数,而不是全局作用域。
You could define it outside of the onload function instead: 您可以在onload函数之外定义它:
var $; // in the global scope
window.onload = function(){
$ = {
ratio: 0.7,
screen: 500,
margin: 10,
canvas: document.getElementById("canvas"),
ctx: document.getElementById("canvas").getContext("2d")
};
console.log($.canvas);
console.log($.ctx);
};
By declaring it in the global scope, it can be accessed from outside of the onload function. 通过在全局范围内声明它,可以从onload函数外部对其进行访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.