rgamber
rgamber

Reputation: 5849

JavaScript colon operator

I am trying to learn JavaScript. After reading this page: What does ':' (colon) do in JavaScript?

I tried to replace

var store = new dojo.data.ItemFileReadStore({
         url: "countries.json"
 });

with

var store = new dojo.data.ItemFileReadStore();
        store.url = "countries.json";

It does not work. Can any one please point out the mistake, or explain the correct use of the Colon operator?. Thanks.

Upvotes: 3

Views: 7148

Answers (6)

Rudu
Rudu

Reputation: 15892

That's not a fair comparison, although you're almost there.

var store = new dojo.data.ItemFileReadStore({
         url: "countries.json"
 });
//Creates a new store object, passing an anonymous object in with URL
// property set to "countries.json"

The alternative without the colon operator is:

var props={};
props.url="countries.json"
var store = new dojo.data.ItemFileReadStore(props);
//Does same as above but doesn't use :

Not this isn't the only use of : in JavaScript though, it can also be used in the ternary operator (alert(b==c?'equal':'not equal');) and in labels (for example in case statements)

Upvotes: 9

rockerest
rockerest

Reputation: 10508

The dojo.data.ItemFileReadStore object probably requires that the url property be present while the object is being created. If that's not the case, then the object doesn't allow you to set that property manually after you have already initialized the object.

The colon is used in JSON to designate the different between a key and a value, when you pass an object structure ({}).

Upvotes: 0

Robert
Robert

Reputation: 21388

If the second way isn't working, you're probably not returning an Object with new dojo.data.ItemFileReadStore(); which prevents you from extending it with dot syntax. If you have an Object, adding to it like that will work fine.

Edit: Misread, in one you're passing an argument, in the other you're assigning to the return value, so two different things, I'll leave the above as an FYI.

Upvotes: 0

Tomasz Kowalczyk
Tomasz Kowalczyk

Reputation: 10467

The first passes url parameter to the so-called constructor or the object, which may do something under the hood with it - for example assign it to other variable or property, for example "url2".

The second assigns url property of that object and you don't know if it will be used.

Upvotes: 2

Dave Ward
Dave Ward

Reputation: 60580

In this case, the object literal in your first example is being used to pass in a set of options to the constructor. Constructing the ItemFileReadStore and then trying to set those options may not be equivalent since it may need them in order to build the object to begin with.

You'd need to do something like this to replace : with =:

var options = {};

options.url = 'countries.json';

var store = new dojo.data.ItemFileReadStore(options);

Upvotes: 0

Pradeep
Pradeep

Reputation: 1254

In first code you are creating a new object and passing it to the function as an argument.

While in second part you are running the function and then, you are setting property of store object. They are totally different thing, as you are not calling function with argument, so it might not run properly. and you are setting return of function to object. not setting property.

Upvotes: 1

Related Questions