SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    need to know the correct terminology

    Hi there,

    I'm working on writing a documentation of something that I was working on and I need to know the correct term for that.
    Code JavaScript:
    function fn()
    {
         return {item1: 100, item2: 200} ;
    }

    I'm sure you have all used the above return type somewhere, but what is this return type called? How would you document the return type for this function? Thanks for your help.
    With Regards
    Pman
    http://www.pmansLab.com

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,014
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)

    array

    The function is returning an array. The syntax would have the same effect as
    arr_name['item1'] = 100;
    arr_name['item2'] = 200;
    although I guess arr_name would depend on how you're using fn()

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    fn returns an object with 2 defined properties-
    var obj=fn();

    obj.itm1=100;
    obj.item2=200;

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    But Mittineague, although it's similar to an associative array in other languages, strictly speaking it's an object and not an array. That's why you create a new one with new Object() (or {}) and not new Array().

    parvez, this might help: http://developer.mozilla.org/en/docs...Guide:Literals

    Although what you are returning there is an object, it's sometimes called an "object literal" to avoid confusion with Javascript's other objects (Array, Function, RegExp, Date, etc.), as it's called in that article I've linked to.

  5. #5
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,014
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)

    object

    Thanks for correcting my somewhat sloppy thinking. I may learn yet

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys. That really helps . I was also thinking something along the line of an object, but didn't know the exact term.

    So, how should I document it? Is there any standards. The reason why I'm asking this is because the returned object literal holds two properties and obviously the user will need to know the name of these properties to be able to access the data. Does the following look correct, if I were to document it like this?

    fn()
    Does something....

    Parameter
    None

    Return Type
    Object Literal
    |> item1: integer
    |> item2: integer
    With Regards
    Pman
    http://www.pmansLab.com

  7. #7
    Google Engineer polvero's Avatar
    Join Date
    Oct 2003
    Location
    Mountain View
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In Python they call them dictionaries - in our case, a dictionary object. And also as someone pointed out, an object literal. An object literal, of course, when written, is known to be authored in 'object notation'. And that, as we all know, is the popular data exchange format JSON. (JavaScript Object Notation)

  8. #8
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you need to document the interface of the object, you could describe an interface (In prosaic terms) and then document the function as returning this interface. Eg.:
    Code:
    /**
    interface Coordinate {
      x : int, y : int
    }
    */
    /**
      *@returns Coordinate
      */
    function fn() {
         return {x: 100, y: 200};
    }
    Alternatively, you could use a constructor, to give the object a type:
    Code:
    Coordinate = function(x, y) {
      this.x = x;
      this.y = y;
    }
    /**
      *@returns Coordinate
      */
    function fn() {
         return new Coordinate(100, 200);
    }
    This will also allow you to use the instanceof operator, to test the type.

  9. #9
    Google Engineer polvero's Avatar
    Join Date
    Oct 2003
    Location
    Mountain View
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I second kyberfabrikken's response. Using this style of commenting also allows you to run your code through a documenter like JSDoc - useful if you're sharing your code with others.


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
  •