【发布时间】:2016-09-04 05:41:58
【问题描述】:
在学习对象原型的过程中,我遇到了下面的例子。
function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
Person.prototype.name = function() {
return this.firstName + " " + this.lastName
};
var myFather = new Person("John", "Doe", 50, "blue");
alert(myFather.name);
当我尝试使用 html 页面通过浏览器运行时,弹出以下警告消息。
function() {
return this.firstName + " " + this.lastName
}
因此,它不是返回“John Doe”,而是将整个函数作为字符串返回。 如何解决?
PS:我使用 VS Code,我使用 node.js live-server 在浏览器中运行。
【问题讨论】:
-
alert(myFather.name());
标签: javascript object prototype