Welcome Guest [Log In] [Register]
Search Members Calendar | Rules ZB Code Index IF Code Index
ZBCode
  • Navigation
  • ZBCode
  • Coding Support
  • Code University
  • Interesting...
Hey, welcome to ZBCode, the premier coding forum for ZB. Here you fill find some of the best Invisionfree and Zetaboards Codes on the network! Unfortunately, you're sorta hovering around in guest mode at the moment; why not join in on the fun? Register an account and you can start accessing the wealth of resources we have available here. Enjoy your stay at ZBCode, and remember to tell all your friends about us; the more members, the more codes available. ;)

Interested in joining? Click here.


If you are already a member of ZBCode, feel free to login right here:

Username:   Password:
Locked Topic
Interesting...
Tweet Topic Started: Jul 11 2009, 10:43 PM (233 Views)
Reid Jul 11 2009, 10:43 PM Post #1
Member Avatar
What? The land of the free? Whoever told you that was your enemy.

Posts:
1,790
Group:
Distinguished Coder
Member
#148
Joined:
Jul 20, 2008
Most people throw around the var keyword when coding javascript, but it has more significance than you may think.

In regular browsers, the keyword is fairly useless, but in the JS standard, look at this:
Code:
 
if (1 == 1) {
x = 1; //global
var z = 1; //global
}
function r() {
d = 1; //global thanks to the lack of the 'var' keyword
var p = 1; //not global because of 'var' keyword
}
// before we call r, d doesn't exist
r();
// d is now a global variable....!
That's right. After you execute a function where you stuck a value in an undefined variable--i.e. didn't use the var keyword first--you have created a global unknowingly!
Code:
 
function some_func() {
var oi = 1;
oi = "55";
oi = parseInt(oi);
}
some_func();
// oi is not global because it was defined in the local scope
Surprising, isn't it? Quite frankly, most people omit the 'var' keyword a lot - but for you sticklers out there like Choco, if you're looking to avoid globals, don't forget the keyword!

The general rule: if a variable isn't defined already, use the 'var' keyword. Remember the shortcut on the keyword as well:
Code:
 
function someRandomFunction() {
var z, y;
z = 1;
y = 1;
}
someRandomFunction();
try {
alert(y);
}
catch(err) {
alert(err); //this will alert "y is not defined", assuming y isn't already global.
}
Just thought I'd bring this to light. :D

The Resource Board
Offline Profile Goto Top
 
Dorith Jul 12 2009, 12:42 AM Post #2
Member Avatar
Has just entered the Matrix

Posts:
2,069
Group:
Former Staff
Member
#1,854
Joined:
Dec 23, 2008
Err... what does global mean exactly? Sorry for intruding on this "high leveled" coding skilled thread...
Posted Image
Posted Image
(Made emoticon using Codes Rock's Smiley Generator)
Offline Profile Goto Top
 
Reid Jul 12 2009, 12:35 PM Post #3
Member Avatar
What? The land of the free? Whoever told you that was your enemy.

Posts:
1,790
Group:
Distinguished Coder
Member
#148
Joined:
Jul 20, 2008
Global variables exist all throughout a page. That's the lesson on scope.
Code:
 
function z() {
var x = 1;
}
z();
// you can't access x here; its scope is in z
var d = 1;
function r() {
alert(d); // alerts "1"
}
r();
// we can access d in any function anywhere because its scope is global
The Resource Board
Offline Profile Goto Top
 
Vitality Jul 12 2009, 02:09 PM Post #4
Member Avatar
Tabula Rasa

Posts:
784
Group:
Former Staff
Member
#320
Joined:
Jul 26, 2008
Hehehe Reid and your scope ^_^ Cool to know though, thanks.
Offline Profile Goto Top
 
Kevin Jul 12 2009, 02:52 PM Post #5
Member Avatar


Posts:
700
Group:
Former Staff
Member
#8
Joined:
Jul 2, 2008
Coding language
HTML/CSS
This topic is beyond the scope of my brain. :P

That's actually quite interesting. Though I never really work with JS, I always sort of wondered what the significance of using var was. :D
Posted Image
Offline Profile Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Code University · Next Topic »
Locked Topic

Track Topic · E-mail Topic Time: 7:46 PM Jul 10
Hosted for free by ZetaBoards · Privacy Policy