JavaScript - Tostring()
I'm average when it comes to math so I'm having trouble understanding the concept of how you convert an integer to a string, and what would be the best outcome? Can someone help me understand the toString() method please?
Similar TutorialsHi, 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... JavaScript has several implicit called function, including toString(), toNumber(), and valueOf(). I was wonderig that what is the prioprity of executing one of these functions when a JavaScript interpreter have a variable to be implicitly type conversed. And my second questions is what are the difference of toString(), toNumber(), and valueOf(). Because ECMAScript does not explain this clearly, is there any other document that I can have further reading? Thx a lot!!!! I have the following code as shown below: Code: <script type="text/javascript"> var a = 12; var b = 35; var c = 56; document.write(a.toString(36).toUpperCase() + "<br>"); document.write(b.toString(36).toUpperCase() + "<br>"); document.write(c.toString(36).toUpperCase() + "<br>"); </script> The problem right now is that for the first 36 number, it always miss the 'prefix 0'. From the code above, it will return the following: Code: C Z 1K My intention was to have all of them display in two character format, as shown below: Code: 0C 0Z 1K May I know how should I do it without having to use if (a < 36) else ... statement? |