Results 1 to 6 of 6

Thread: Please help me understand how a function can use an object as a parameter.

  1. #1
    Join Date
    Oct 2012

    Please help me understand how a function can use an object as a parameter.

    I am following a tutorial from a book. The tutorial is pretty straightforward. There is some html code which has embedded javascript and there is also an external javascript file. Basically, the website uses javascript to get the current date and time and display it in the correct field, but before doing that it uses an external javascript file to extract the current date and time as separate values and put them together as a string. This string will then be put into the corresponding field on the webpage.

    Sorry for going into the long description, but I felt like I have to explain what I am doing so its easier to understand.

    The issue I am having is with the external file. It uses an object as a parameter for a function, even though the object "seemingly" doesn't exist anywhere! How is this possible? This is driving me nuts. I spend many hours looking over the code trying to find out where this parameter exists that they are passing in, and still I cannot figure it out.

    This is the htm file
    <!DOCTYPE html>
          New Perspectives on HTML, CSS, and JavaScript
          Tutorial 11
          Tutorial Case
          New Year's Countdown Clock
          Date:  7/2/2013 
          Filename:         clock.htm
          Supporting files: banner.png, functions.js, 
                            modernizr-1.5.js, newyear.css
          <meta charset="UTF-8" />
          <title>Tulsa's New Year's Bash</title>
           <script src="modernizr-1.5.js" type="text/javascript" ></script>
          <link href="newyear.css" rel="stylesheet" type="text/css" />
           <script type="text/javascript" src="functions.js"></script> 
           <script type="text/javascript">
               function NYClock() {
                   //the today variable contains the current date and time
                   var today = new Date("February 24, 2015 14:35:05");
                   //display the current date and time
                   document.clockform.dateNow.value = showDate(today);
                   document.clockform.timeNow.value = showTime(today);
                   //calculate the time left until new year's eve bash
                   document.clockform.daysLeft.value = "99 day(s)";
                   document.clockform.hrsLeft.value = "99 hour(s)";
                   document.clockform.minsLeft.value = "99 minute(s)";
                   document.clockform.secsLeft.value = "99 second(s)";
       <body onload="NYClock()">
          <form name="clockform" id="clockform" action="">
             <h1>New Year's Eve<br />Bash</h1>
             <div id="time">
                <h2>Hello Tulsa</h2>
                <input id="dateNow" name="dateNow"  />
                <input id="timeNow" name="timeNow" />
             <div id="countdown">
                <h2>Watch the Countdown</h2>
                <input id="daysLeft" name="daysLeft"  />
                <input id="hrsLeft" name="hrsLeft"  /> 
                <input id="minsLeft" name="minsLeft"  /> 
                <input id="secsLeft" name="secsLeft"  /> 
             <img src="banner.png" alt="" />
          <nav class="horizontal">
                <li><a href="#">News</a></li>
                <li><a href="#">Entertainment Beat</a></li>
                <li><a href="#">Tulsa Times</a></li>
                <li><a href="#">Event Calendar</a></li>
                <li><a href="#">City Links</a></li>
          <nav class="vertical">
             <h1>Links to the Party</h1>
                <li><a href="#">FrostiRun</a></li>
                <li><a href="#">Kid's Korner</a></li>
                <li><a href="#">Chester the Jester</a></li>
                <li><a href="#">A Taste of Tulsa</a></li>
                <li><a href="#">Winter Art Fair</a></li>
                <li><a href="#">Lester's Blues Band</a></li>
                <li><a href="#">The Jazz Express</a></li>
                <li><a href="#">The Fireworks Show</a></li>
                <li><a href="#">Final Countdown</a></li>
          <section id="middle">
             <p>Tulsa's New Year's Eve Bash is back for the 22nd year of fun and 
                excitement. Be sure to arrive 
                early for all of the festivities and the final countdown.
                The party starts on December 31 at noon with 
                the <em>FrostiRun</em> 5K and 10K races. Sign up now or the morning of 
                the race.
             <p>Family fun is available at the <em>Kid's Korner</em> with 
                booths and free activities for the whole family, including 
                performances from Tulsa's own <em>Chester the Jester</em>.
          <section id="right">
             <p>The fun continues with <cite>A Taste of Tulsa</cite> 
                from Tulsa's best eateries, and the artistically inclined should
                plan on attending the <cite>Winter Art Fair</cite> on Tulsa's 
                downtown square.
             <p>Music from <cite>Lester's Blues Band</cite> and <cite>The Jazz 
                Express</cite> brings us into the New Year. The dancing starts 
                at 7 p.m. and continues until midnight. From 10 to 11 p.m. we 
                say goodbye to the old year (and ring in the new) with the 
                famous New Year's Eve Bash fireworks spectacular. 
                New Year's Eve Bash  
                340 Main Street, Tulsa, OK 74102 
                (918) 555-3481

    This is the external .js file

       New Perspectives on HTML, CSS, and JavaScript
       Tutorial 11
       Tutorial Case
       Date:   8/7/2013
       Function List:
          Returns the current date in the format mm/dd/yyyy
          Returns the current time in the format hh:mm:ss a.m./p.m.
          Returns the number of days between the current date and January 1st
          of the next year
    function showDate(dateObj){
        thisDate = dateObj.getDate();
        thisMonth = dateObj.getMonth() + 1;
        thisYear = dateObj.getFullYear();
        return thisMonth + "/" + thisDate + "/" + thisYear;
    function showTime(dateObj){
        thisSecond = dateObj.getSeconds();
        thisMinute = dateObj.getMinutes();
        thisHour = dateObj.getHours();
        return thisHour + ":" + thisMinute + ":" + thisSecond;
    Do you see the dateObj that they are passing in? This dateObj doesn't exist anywhere in the html file or the javascript file. How can a function use methods to extract something from an object when this object was never created or defined?

    Thanks very much.
    Last edited by ibex333; 08-08-2013 at 06:18 PM.

  2. #2
    Join Date
    Jun 2008
    Obviously they create an object somewhere. It's probably just new Date(), which is an object.
    I can't know for sure since you didn't post the external file, so... yeah.

  3. #3
    Join Date
    Oct 2012
    Yes! You are correct. But how does new Date() create dateObj? the new date, as you can see in the htm file, was written to the "today" variable. So shouldnt it be "today" that will be passed in and not dateObj? Sorry.. I am completely lost.

    Here, I'll post both files. Look at my OP.

    Whats interesting, is that I can call that parameter whatever.... it doesn't have to be called "dateObj"... and it still works fine!
    Last edited by ibex333; 08-08-2013 at 06:22 PM.

  4. #4
    Join Date
    Jun 2008
    var today = new Date("February 24, 2015 14:35:05");
                   //display the current date and time
                   document.clockform.dateNow.value = showDate(today);
                   document.clockform.timeNow.value = showTime(today);
    This code in NYClock() creates a new Date() object, and passes that object to showDate and showTime. The functions receive a reference to the object so that they can access it.
    I think you're just confused on parameter/argument names. I'm not very good at explaining, but I'll try.

    The parameter can be named anything. It doesn't have to relate to the variable name that was passed. Regardless of what the variable name was that contained the date object, showTime & showDate will rename it to 'dateObj' and use it like that.

    function process(dog) {
        // process() was passed a reference to the string stored
        // in the variable 'cat'
        // this parameter can be named anything. it's just a reference
        return dog;
    var cat = 'persian';
    I hope you understand. I'm not the best at explanations.

  5. #5
    Join Date
    Oct 2012
    I still dont quite understand, but I think you already pointed me in the right direction....

    Does the relating topic in javascript have something to do with "pass by reference" and "pass by value"? I want to read up on this because I am very shaky on this topic.
    Last edited by ibex333; 08-08-2013 at 07:14 PM.

  6. #6
    Join Date
    Jun 2008
    Yea I see how it could be confusing at first. I don't know what you would look up specifically for that. It's just something you'll have to get used to (the difference between parameters/arguments and variables).

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.11874 seconds
  • Memory Usage 2,912KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (4)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (6)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates