使用JavaScript中的“获取”将属性作为属性访问?
在JavaScript中 使用getter,我们可以通过其他方式将属性作为属性进行访问。关键字“get”用于完成此过程。让我们简要地讨论一下。
示例
在下面的示例中,属性'details'用作函数'details()'。因此,我们不会获得任何结果。相反,如果我们删除括号,关键字'get'确保将属性“details”视为属性而不是函数。
<html>
<body>
<p id = "prop"></p>
<script>
var business = {
product: "Tutorix",
parent : "Nhooo",
get details() {
return this.product+" is the product of" + " " + this.parent;
}
};
document.getElementById("prop").innerHTML = business.details();
</script>
</body>
</html>示例
在这里,由于括号中的 括号已删除,因此'get'关键字可确保将属性 “details”视为属性 而不是函数。
<html>
<body>
<p id = "prop"></p>
<script>
var business = {
product: "Tutorix",
parent : "Nhooo",
get details() {
return this.product+" is the product of" + " " + this.parent;
}
};
document.getElementById("prop").innerHTML = business.details;
</script>
</body>
</html>输出结果
Tutorix is the product of Nhooo
热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短