Hi,
I was just wondering how this statement is working in javascript.
this.toString = toString;
calling a function from the other function, up to my knowledge should be like : this.toString = toString();, but this is not working.
Could someone please help me to clarify?
function Car()
{
this.make = new Array();
this.colors = new Array();
this.price = new Array();
this.year = new Array();
this.model = new Array();
this.toString = toString;
}
function toString()
{
return ("Make: " + this.make "\n" "Color: " this.colors "\n" "Year: " this.year "\n" "Model: " this.model "\n" "Price: " this.price "\n");
}
// Create an instance of a Car
BMW = new Car();
BMW.make= "BMW";
BMW.colors[0] = "Blue";
BMW.price = "$50000";
BMW.year = 2006;
BMW.model = "Z4";
document.write(BMW.toString());
MyModel = BMW.model;
document.write(MyModel);
//Add more colors
BMW.colors[1] = "Red";
BMW.colors[2] = "Brown";
BMW.colors[3] = "Black";
document.write(BMW.toString());
I was just wondering how this statement is working in javascript.
this.toString = toString;
calling a function from the other function, up to my knowledge should be like : this.toString = toString();, but this is not working.
Could someone please help me to clarify?
function Car()
{
this.make = new Array();
this.colors = new Array();
this.price = new Array();
this.year = new Array();
this.model = new Array();
this.toString = toString;
}
function toString()
{
return ("Make: " + this.make "\n" "Color: " this.colors "\n" "Year: " this.year "\n" "Model: " this.model "\n" "Price: " this.price "\n");
}
// Create an instance of a Car
BMW = new Car();
BMW.make= "BMW";
BMW.colors[0] = "Blue";
BMW.price = "$50000";
BMW.year = 2006;
BMW.model = "Z4";
document.write(BMW.toString());
MyModel = BMW.model;
document.write(MyModel);
//Add more colors
BMW.colors[1] = "Red";
BMW.colors[2] = "Brown";
BMW.colors[3] = "Black";
document.write(BMW.toString());