Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling a function from other function in JS

Status
Not open for further replies.

diggermf

Programmer
Jun 16, 2008
20
US
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());
 
Hi

diggermf said:
this is not working.
How could that work ? You missed 13 concatenation operators.
Code:
[b]function[/b] [COLOR=darkgoldenrod]toString[/color][teal]()[/teal]
[teal]{[/teal]
  [b]return[/b] [teal]([/teal][green][i]"Make: "[/i][/green] [teal]+[/teal] [b]this[/b][teal].[/teal]make [teal][highlight]+[/highlight][/teal] [green][i]"[/i][/green][lime][i]\n[/i][/lime][green][i]"[/i][/green] [teal][highlight]+[/highlight][/teal] [green][i]"Color: "[/i][/green] [teal][highlight]+[/highlight][/teal] [b]this[/b][teal].[/teal]colors [teal][highlight]+[/highlight][/teal] [green][i]"[/i][/green][lime][i]\n[/i][/lime][green][i]"[/i][/green] [teal][highlight]+[/highlight][/teal] [green][i]"Year: "[/i][/green] [teal][highlight]+[/highlight][/teal] [b]this[/b][teal].[/teal]year [teal][highlight]+[/highlight][/teal] [green][i]"[/i][/green][lime][i]\n[/i][/lime][green][i]"[/i][/green] [teal][highlight]+[/highlight][/teal] [green][i]"Model: "[/i][/green] [teal][highlight]+[/highlight][/teal] [b]this[/b][teal].[/teal]model [teal][highlight]+[/highlight][/teal] [green][i]"[/i][/green][lime][i]\n[/i][/lime][green][i]"[/i][/green] [teal][highlight]+[/highlight][/teal] [green][i]"Price: "[/i][/green] [teal][highlight]+[/highlight][/teal] [b]this[/b][teal].[/teal]price [teal][highlight]+[/highlight][/teal] [green][i]"[/i][/green][lime][i]\n[/i][/lime][green][i]"[/i][/green][teal]);[/teal]
[teal]}[/teal]


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top