I have this javascript code below:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
<script>
function Person(first, last) {
this.first = first;
this.last = last;
}
Person.prototype.toString = function() {
return this.first + this.last;
}
var person = new Person("John", "Dough");
alert(person); // same result since alert calls toString()
</script>
</head>
<body>
</body>
</html>
The question is why does the alert(person) display "JohnDough"? To me, alert(person) should not display anything.
alertcallstoString()of the passed object, just like the comment says: alert calls toString(). AndtoStringis overwritten on the prototype to concatenate the first and last name.