SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,595
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)

    Inheriting from Date

    Is there any way at all to create a new template object that inherits from the built in Date object so as to be able to add new methods to that child object without adding them to the built in Date object? I've tried everything I can think of and as far as I can tell it keeps referencing the Date function instead of the Date object and so doesn't work.
    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="^$">

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure if I've understood and I'm not saying there isn't some fatal fallacy associated with it, but does this fit the bill?

    Code:
    <script type='text/javascript'>
    
    Object.prototype.MyDate = Date;
    
    Object.MyDate.prototype.showTime = function()
    {
      alert( this.getTime() ); /* Method inherited from Date */
    }
    
    md = new Object.MyDate();
    
    md.showTime();
    
    alert( Date.showTime ); /* undefined so has not affected Date object */
    
    </script>
    Tab-indentation is a crime against humanity.

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,595
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    If that works then it is adding extra methods to all objects where what I am trying to do is to avoid adding extra methods to all Dates and just add them to specific dates
    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="^$">

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,595
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    I came up with a solution but it is a bit messy. Basically create a Date as a private property within the new object and map all the existing date methods to that private date object before adding all the extra methods. See http://javascriptexample/dollarD.php for what I have managed to do.

    I still don't like how messy it is in actually getting the built in date methods to work so if anyone has any suggestions for a workable alternative.

    At least I achieved my intended objective of not adding the extra methods to the Date object.
    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="^$">


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
  •