Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2004
    Location
    London, England
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Newbie. basic misundersanding of language structure

    Code:
       exp = new Date()
       oneYearFromNow = exp.getTime() + (365*24*60*60*1000)
       exp.setTime(oneYearFromNow)
       gmt = exp.toGMTString()
    In this copied example, the third line is not an assignment yet sets a property of EXP. Why do we sometimes require not to have the =?

    Clearly this is a daft question the answer to which should be on about page 1 of the book. Yet RTFM has not given me the answer. Sorry, everyone.

    Next question; how can I combine 4 lines to 1 to set the variable GMT? Or can't I?

  • #2
    Regular Coder Vincent Puglia's Avatar
    Join Date
    Jul 2003
    Location
    where the World once stood
    Posts
    367
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Hi,

    'setTime(...)' is a built in javascript function that, as its name says, sets a value (the time) for the variable that preceeds the dot (exp). Somewhere within the built in function there is an "=" assignment operator. [Many, if not all of the built in functions, do assignments.]

    I just woke up and haven't had my mugs of espresso yet, so in re to your last question: In general, you can combine lines by placing functions within functions.

    For example, you should be able to combine lines 2 & 3, as in:

    exp.setTime( (exp.getTime() + (365*24*60*60*1000) )

    but I've never tried to combine lines simply for the sake of doing so. What would be the point? It would only make the code the hard to follow and later debug.



    Vinny
    Where the world once stood
    the blades of grass
    cut me still

  • #3
    New Coder
    Join Date
    Jan 2004
    Location
    London, England
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your help, Vinnie

    Originally posted by Vincent Puglia
    'setTime(...)' is a built in javascript function that, as its name says, sets a value (the time) for the variable that preceeds the dot (exp). Somewhere within the built in function there is an "=" assignment operator. [Many, if not all of the built in functions, do assignments.]
    Hmm, but not all; it's just an RTFM problem to work out which do or don't? Is there a general rule? For instance, I don't think toGMTString does.
    but I've never tried to combine lines simply for the sake of doing so. What would be the point? It would only make the code the hard to follow and later debug.
    Quite so; I'm very new and want to learn what I can and can't actually do in this (big!?) language; real-life style is of course KISS.

  • #4
    Regular Coder Vincent Puglia's Avatar
    Join Date
    Jul 2003
    Location
    where the World once stood
    Posts
    367
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Hi,

    You seem to be delving too deeply. Javascript is a relatively simple c-like language. If you pick up and read O'Reilly Publisher's Javascript: a Definitive Guide, many of your questions will be answered.

    General rule is: when in doubt, see how other people use a function.

    Vinny
    Where the world once stood
    the blades of grass
    cut me still


  •  

    Posting Permissions

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