html - Where to initialize the global variable that will be accessed by many functions in JavaScript - Stack Overflow

I am very new in JavaScript. I want to develop a canvas animation to run on Blackberry. For that I woul

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

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
Share Improve this question edited Mar 31, 2021 at 10:42 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 14, 2011 at 12:01 miafmiaf 412 silver badges8 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Any javascript variable defined in the global scope (i.e. not in a function or class) is accessible from the rest of the code.

var testVariable = "test";
function test() {
    console.log(testVariable);        
}
test();

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:

function test() {
    testVariable = "test";
}

test();
console.log(testVariable);

edit:

As the ment rightly points out:

Global variables and functions are rarely required. Using globals may cause naming conflicts between javascript source files and cause code to break. For this reason, it is a good practice to encapsulate functionality within a single global namespace...The simplest approach is to create a single global object and assign properties and methods to this object.

Creating A Namespace:

var MyLib = {}; // global Object cointainer
MyLib.value = 1;
MyLib.increment = function() { MyLib.value++; }
MyLib.show = function() { alert(MyLib.value); }

MyLib.value=6;
MyLib.increment();
MyLib.show(); // alerts 7

Here's a description of what a namespace is.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744662939a4586573.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信