使用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
热门推荐
1 潜能开发训练狗狗
2 狗狗多会训练好
3 扣篮训练狗狗
5 狗狗不训练
6 少儿柔术训练狗狗
8 训练好狗狗
9 训练狗狗怎么训
10 狗狗训练猫咪
11 狗狗训练地点
12 两个多月的狗狗怎么训练
13 怎么训练狗狗去厕所拉屎
14 狗狗怎样训练大小便
15 训练狗狗母
16 怎么训练狗狗坐下和握手
17 近视训练狗狗
18 狗狗倒的训练