Method or class?

Hi,

I have a definition of a method as a function which belongs to an object, and I have the code below. But I think the book is telling me that the function is a class. So, is it both a class and a method?

var star = {}

function Star(constell,type,specclass,magnitude) {
this.constellation = constell;
this.type = type;
this.spectralClass = specclass;
this.mag = magnitude;
}

star[“Polaris”] = new Star(“Ursa Minor”,“Double/Cephid”,“F7”,2.0);
.
.
.
Oh, wait, I think I get it…In the function, “Star” begins with upper-case, where in the first line, and the "star[“Polaris”] line, it’s lowercase. So, now, I think that’s telling me that I could have called the function anything – “SAM”, for instance, and then I would have coded star[“Polaris”]=new SAM…

Just typing this here, I think shined the light for me. If so, it’s a subtle difference in the book, that I wish the author had pointed out.

Thks,
Jeff

JavaScript doesn’t have classes - it only has objects.

A method of one object in JavaScript can be another object.

There is a convention that objects that are being used as if they were classes be named starting with a capital letter (that is done by people used to object oriented languages who try to write JavaScript as if it were an object oriented language instead of like the prototyping language that JavaScript actually is).

Yup, you hit the nail on the head there :tup:

Thanks guys,

Well, quoting from page 139 of “Javascript Step by Step” by Steve Suehring:
“Classes define sets of objects that share the same properties and methods. Classes simplify the creation of multiple objects of the same type.”

So, perhaps he’s just trying to keep things simple for us newbies…I already have enough trouble rolling all this around in my mind. Maybe later, when we’re more comfortable with it, he’ll spring on us that it really wasn’t a class afterall! :slight_smile:

Btw, to me, this looks a lot like what I’d consider an array – all these “new Objects” that have the same properties…
Thanks again,
Jeff

Or perhaps, as may be more likely, he’s mixing in his experience from other languages.

For some javascript-specific info on how class-based objects compare with JavaScript’s prototype-based objects, Details of the object model is a pretty good read.