SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2010
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,604
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    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).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,900
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by jefals View Post
    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...
    Yup, you hit the nail on the head there
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2010
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!

    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

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by jefals View Post
    So, perhaps he's just trying to keep things simple for us newbies...
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •