我写的继承的短代码reader来自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>
Run Code Online (Sandbox Code Playgroud)
或者,我可以删除行 Person.prototype.getName = function() { return this.name; }并在Person对象中创建它.例如
<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>
Run Code Online (Sandbox Code Playgroud)
getName()在这两种情况下调用时,我得到了相同的结果.那他们有什么不同?