Jump to content

Bracket Conventions in Guides?


Catfish

Recommended Posts

Hi there, I have been going through the jQuery guides on w3schools and came to a point where I wondered about the conventions for different brackets used in the guides. http://www.w3schools...ery_effects.asp Up until the section on jQuery Custom Animations, all function defitions were written like: $(selector).fadeTo(speed,opacity,callback) where no brackets were placed around the parameters for functions. For the animate() function, this changed: $(selector).animate({params},[duration],[easing],[callback]) At first I thought this meant the guide was detailing that the parameter params was required by using curly brackets { } and the other three were option using square brackets [ ].But then as I read on, I noticed the code had the curly brackets in it:

<script type="text/javascript">$(document).ready(function(){  $("button").click(function(){	$("div").animate({height:300},"slow");	$("div").animate({width:300},"slow");	$("div").animate({height:100},"slow");	$("div").animate({width:100},"slow");  });});</script>

So then I wondered, why are the square brackets there? Are there any conventions used in the w3school guides for syntax like this? I guess maybe not because with many languages a single convention might not fit all? Also, I couldn't find this outlined in the jQuery guide, but when passing arguments to a function, what are the rules in jQuery for using quotes? So far from teh guides the impression I get is, "numbers - don't use quotes. everything else - use quotes." Is this correct?

Link to comment
Share on other sites

Generally, curly brackets represent an object, while square brackets represent arrays. They're representations of objects and arrays in Javscript syntax. Here's an example of how they're used in ordinary code:

var obj = {  property1 : "value1",  property2 : "value2"}var arr = [  "element1",  "element2"]

If no brackets are there then it means scalar data, or data that has just one single value like a number, string or boolean. It seems, though, that W3Schools doesn't have a particular convention because it looks like square brackets indicate an optional parameter.

Link to comment
Share on other sites

Square brackets are often used to show optional parameters, I've never seen a function definition in a reference use curly brackets to indicate an object. If the data type is given in the function definition it usually precedes the parameter name. Check here for an example of data types and an optional parameter: http://www.php.net/m...tion.substr.php If a function has multiple optional parameters you may also see it expressed like this: function func (obj a [, int b [, int c [, int d]]]) That convention is used to indicate that you wouldn't be able to have the d parameter without the b and c.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...