gpt4 book ai didi

javascript - 对面向对象的javascript的困惑

转载 作者:行者123 更新时间:2023-12-02 06:07:49 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com