I wrote short code of inheritance of reader from Person:
<script>
/* Class Person. */
function Person(name) {
this.name = name;
}
Person.prototype.getName = function() {
return this.name;
}
var reader = new Person('John Smith');
alert(reader.getName());
</script>
Alternatively I can delete the line of Person.prototype.getName = function() { return this.name; } and create it in the Person object. For example
<script>
/* Class Person. */
function Person(name) {
this.name = name;
this.getName = function() { return this.name;}
}
var reader = new Person('John Smith');
alert(reader.getName());
</script>
I got the same result when invoking getName() in both these cases. So how are they different?