noob question: what's a { doing there?

G

Guest

I'm a total noob, and I'm trying to understand this code:

var newsi = {
name:"newsi",
dom:false
};

newsi.Client=function(){
//stuff
}

I can't work out what datatype newsi is, I know you're gonna say
object but shouldn't it be one of number, string, boolean, null,
function, error, or something like that? It looks like a dictionary to
me but can a dictionary have a method called Client?

It's at the start of this file btw http://news.bbc.co.uk/js/newsi/latest/newsi.js?8


I've also seen: var foo = {};
That would be an empty.... what?


Apologies for my ignorance.

Thanks for your time in advance.
 
V

VK

I'm a total noob, and I'm trying to understand this code:

var newsi = {
name:"newsi",
dom:false

};

newsi.Client=function(){
//stuff

}

I can't work out what datatype newsi is, I know you're gonna say
object but shouldn't it be one of number, string, boolean, null,
function, error, or something like that?

Not necessarily. There can be Ford, Mazda, Ferrari - but all of them
are cars. Same can be Date, Array, Boolean but all of them also
extended in this or that way Object.
Also please note that numbers, strings, booleans and null are
primitives in Javascript, not objects (though you may use object
wrappers for them).

In your case you have new Object instance creation with some
properties and methods immediate instantiation over assignment.
Equivalent effect with longer but more clear syntax would be:

var newsi = new Object;
newsi.name = "newsi";
newsi.dom = false;
newsi.Client=function(){
//stuff
}

Don't forget that in Javascript object structure is dynamic and can be
changed at any time in any way even after the object's creation.
 
D

dd

I'm a total noob, and I'm trying to understand this code:

They're using the newer style of Javascript syntax called JSON
(JavaScript Object Notation). If you google JSON you'll find a lot of
info on it. It's simply a different way of defining objects and their
properties/methods. The result though is identical to what would be
created by that code example you see in VK's answer. JSON has been
supported since about 1999 but only really became popular in the last
3 years or so.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,006
Latest member
LauraSkx64

Latest Threads

Top