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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    this.tostring = function ()

    Hi,

    I was looking at this code from another thread (code by Old Pendant)

    <script type = "text/javascript">
    // constructor for a Car ojbect:
    function Car( reg, make, vin )
    {
    this.registration = reg;
    this.manufacturer = make;
    this.VIN = vin;
    this.toString = function() {
    return "\nregistraion: " + this.registration
    + ", make: " + this.manufacturer
    + ", vin: " + this.VIN; };
    }

    // Purpose: Gather car information and store it in a datbase
    // Ask user for REG info, CAR MAKE, car VIN
    var howMany = prompt("Enter details for how many cars?", "");

    var cars = [];

    for (var i = 1; i <=howMany; i++) {
    var r = prompt ("Enter Registration Number for car No." + i,"");
    var m = prompt ("Enter Car Make for car No." + i,"");
    var v = prompt ("Enter Car VIN Number for car No." + i,"");
    cars.push( new Car( r, m, v ) );
    }

    alert( cars );
    </script>

    What I want to know is what this.tostring = function () does? And if this can be removed because with this there I can't put the code into a bigger function.

    I hope that has made some kind of sense, any help is appreciated...

  • #2
    Regular Coder
    Join Date
    Jan 2008
    Location
    Geneva, Switzerland
    Posts
    413
    Thanks
    12
    Thanked 29 Times in 29 Posts
    No, it does not make sense, because we have no idea what you are trying to do.
    Chuck Norris counted to infinity.
    Twice.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,927
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Is it not obvious? It creates a concatenated string consisting of the reg, make and vin of all the cars for which details have been entered.

    The chap is Old Pedant, not Old Pendant!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Regular Coder
    Join Date
    Jan 2008
    Location
    Geneva, Switzerland
    Posts
    413
    Thanks
    12
    Thanked 29 Times in 29 Posts
    Well ...
    Chuck Norris counted to infinity.
    Twice.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    if this can be removed because with this there I can't put the code into a bigger function.
    Of course it can be removed. It's only purpose is so I could write alert(cars); and have the result be something readable.

    But I also can't figure out in what way it would *possibly* interfere with a "bigger" function. That's a constructor for a Car object. Unless you mean you are going to add a lot of other methods to that constructor, that function should have ZERO impact on other code.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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