Jump to content

Spaces in code


kurt.santo
 Share

Recommended Posts

Does it matter if you have space or not in your code?So, is:

	init : function()	{ 						if ( !document.getElementById || !document.createTextNode ){return;}						var lists = document.getElementsByTagName( 'ul' );						for( var i = 0, i < lists.lenght; i++ )	{							if( !DOMhelp.cssjs( 'check', lists[i], minislides.triggerClass )){continue;}							minislides.initShow( lists[i] );						}						},

the same as:

	init:function(){		if(!document.getElementById || !document.createTextNode){return;}		var lists=document.getElementsByTagName('ul');		for(var i=0;i<lists.length;i++){			if(!DOMhelp.cssjs('check',lists[i],minislides.triggerClass)){continue;}			minislides.initShow(lists[i]);		}	},	

?I have a spendid new code and typed it myself again (makes learning easier for me). I cannot find any mistake in my complete code (longer than given extract) and wondered about the spaces...KurtBTW: Corrected already the typo in first code box (line "for( var i = 0, i < lists.lenght; i++ )"). It is now as it should ("var i = 0; etc ")...

Link to comment
Share on other sites

Whitespace doesn't matter as long as you don't break up a token with it. The colon between init and function might not be able to have spaces around it, I'm not sure. Also the class member reference operator (.) can't have spaces around it. But as long as you aren't breaking up a token you can have however many spaces or newlines or tabs you want.

Link to comment
Share on other sites

Whitespace doesn't matter as long as you don't break up a token with it. The colon between init and function might not be able to have spaces around it, I'm not sure. Also the class member reference operator (.) can't have spaces around it. But as long as you aren't breaking up a token you can have however many spaces or newlines or tabs you want.
Which one is the class member reference operator (.) ? Kurt
Link to comment
Share on other sites

Which one is the class member reference operator (.) ? Kurt
The "class member reference operator", as specified in the brackets is the "dot" character, i.e. the same character that goes between "w3schools" and "com" to form "w3schools.com".Where do you see dots in your code? Those dots are such operators.
Link to comment
Share on other sites

Right, for example document is an object (or class) that has members (properties and methods). To refer to a certain member, like getElementById, you do document.getElementById. I'm pretty sure that would not work with spaces around the dot.
Cheers. So the class member reference operator is the dot, after which the relevant property or method of a given object/class is listed. Makse sense that this one should not have a space....Kurt
Link to comment
Share on other sites

Does it matter if you have space or not in your code?
Whitespace doesn't matter as long as you don't break up a token with it.
As justsomeguy says, whitespace does not affect the execution of the code as long as you don't break up a token with it. However, on high volume sites, consider the difference between the following:
init:function()			   {									   alert("Hello");								   }

init:function(){	alert("Hello");}

The first block has roughly 50 more bytes in unnecessary white-space. If you were to serve that code 10,000 times a day, that's around 500KB a day in wasted bandwidth - 180MB a year - just in the whitespace for a three-line function.Imagine your whole site set up with that much whitespace.

Link to comment
Share on other sites

As justsomeguy says, whitespace does not affect the execution of the code as long as you don't break up a token with it. However, on high volume sites, consider the difference between the following:
init:function()			   {									   alert("Hello");								   }

init:function(){	alert("Hello");}

The first block has roughly 50 more bytes in unnecessary white-space. If you were to serve that code 10,000 times a day, that's around 500KB a day in wasted bandwidth - 180MB a year - just in the whitespace for a three-line function.Imagine your whole site set up with that much whitespace.

Good point Jesh. I will keep this in mind :-)Kurt
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...