我在 javascript 中练习面向对象的语法,但遇到了一些问题。这是我的代码:
<html>
<head>
<script type="text/javascript">
function Name(first,mid,last) {
this.first = first;
this.middle = mid;
this.last = last;
}
Name.prototype.fullname = function () {
return this.first + " " + this.middle + " " + this.last;
}
Name.prototype.fullnamereversed = function() {
return this.last + " " + this.middle + " " + this.first;
}
var s = new Name("James","Harlow","Smith")
</script>
</head>
<body>
<script type="text/javascript">
document.body.innerHTML = s.fullname;
document.body.innerHTML = s.fullnamereversed;
</script>
</body>
</html>
当我加载页面时,body 的 innerHTML 是 Name.protoype ("function ()... this.first + this.middle + this.last...") 的确切文本。我在这里做错了什么?
最佳答案
您需要使用 () 运算符调用函数:
document.body.innerHtml = s.fullname();
关于javascript - 对面向对象的javascript的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6916486/