在哪里初始化将被 JavaScript 中的许多函数访问的全局变量
我对 JavaScript 很陌生。我想开发一个在黑莓上运行的画布动画。为此我想使用 HTML5 和 JavaScript。我想创建以下函数:
function
drawCircle()
。圆的中心将是画布的中心(画布的大小将是窗口的大小),半径将由用户输入。到目前为止,我应该在哪里声明画布才能指定圆心?function
draw()
将所有绘制形状的函数。然后会在init函数中调用。函数
init()
。这将以一组间隔绘制形状。
我应该在哪里声明这些?:
var canvas = document.getElementById()
var context = canvas.getContext()
canvas.width = windows.innerWidh
>
I am very new in JavaScript. I want to develop a canvas animation to run on Blackberry. For that I would like to use HTML5 and JavaScript. I want to create the following functions:
function
drawCircle()
. The center of the circle will be the center of the canvas (the size of the canvas will be the size of the window), the radius will be enter by the user. Up to here, where should I declare the canvas in order to assign the center of the circle?function
draw()
which will all functions which draw shapes. Then will be called in the init function.function
init()
. Which will draw the shapes at a set of interval.
Where should I declare these?:
var canvas = document.getElementById()
var context = canvas.getContext()
canvas.width = windows.innerWidh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在全局范围内(即不在函数或类中)定义的任何 JavaScript 变量都可以从代码的其余部分访问。
或者(这被认为是不好的做法)在全局作用域之外声明一个不带
var
修饰符的变量,将其放入全局作用域中:edit:
正如评论正确指出的那样出去:
创建命名空间:
以下是什么是命名空间的描述。
Any javascript variable defined in the global scope (i.e. not in a function or class) is accessible from the rest of the code.
Alternatively (and it's frowned upon as bad practice) declaring a variable without the
var
modifier from outside the global scope puts it in the global scope:edit:
As the comment rightly points out:
Creating A Namespace:
Here's a description of what a namespace is.