View Full Version : this.tostring = function ()

12-12-2011, 09:54 AM

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 );

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...

12-12-2011, 11:06 AM
No, it does not make sense, because we have no idea what you are trying to do.

Philip M
12-12-2011, 12:18 PM
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! ;)

12-12-2011, 12:47 PM
Well ... :D

Old Pedant
12-12-2011, 09:17 PM
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.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum