05.01.2013 Views

hide - Understanding jQuery

hide - Understanding jQuery

hide - Understanding jQuery

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Understanding</strong> JSON Definition<br />

While writing new episodes for my <strong>jQuery</strong> Tutorial newsletter many people have asked me<br />

why some variables are defined using quotation marks and why others don’t.<br />

In particular with regard to the animate function example from the previous page, I was asked<br />

the question: Why are there quotation marks around the argument “linear” but not around the<br />

integer whose value is 1500?<br />

We have already talked about the fact that JavaScript function parameters are typeless. This<br />

means that you can pass any type of an argument to them. An argument can be a string (text<br />

enclosed using quotation marks) or an integer (simply a digit.) JavaScript functions parameters<br />

do not determine or ask for the obeject type passed to them like C++ functions do.<br />

The assumed type of the parameter is determined by the programmer, not by the language<br />

specification. This means that when constructing a function of your own you are responsible<br />

for determining the type of the arguments that presumably will be passed to that function.<br />

It’s your responsibility to write the function in such way that handles any possible type of a<br />

parameter passed to it. <strong>jQuery</strong> programmers made the decision that the first parameter of the<br />

function animate() should be a JSON, the second an integer, and the third a string.<br />

JSON is the syntax rule for defining an object in JavaScript. An object can contain a number of<br />

variables, functions or other objects. Let’s take a look at an incredibly basic example of JSON<br />

object definition:<br />

var obj = { a: 1, b: 2 };<br />

It’s important to note that neither var, nor obj are referred to as JSON. JSON is in itself only a<br />

guideline for object definition in JavaScript as seen between the two { and } brackets. One of<br />

the most important things about JSON is that it uses the : (colon symbol) and the , (comma) to<br />

define the inner contents of an object. In other words, that’s just the way the syntax was defined<br />

by the creators of JavaScript language specification (Originally known as EcmaScript.)<br />

JSON in itself is a unique data representation like var or function. Except it has no keyword<br />

definition. In other words { var a: 1, var b: 2 }; would be a wrong way to create an object and<br />

this would create a JavaScript syntax error. This is important because there is actually a second<br />

way of creating objects in JavaScript using the function keyword where defining a using<br />

var a = 1; would actually be legal, but we will discuss this in another section of this book.<br />

35

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!