使用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 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短