getter 사용 자바스크립트 다른 방식으로 속성에 속성으로 액세스할 수 있습니다. 키워드 'get '는 이 과정을 수행하는 데 사용됩니다. 간단히 이야기해 보겠습니다.
예시
다음 예에서 속성 'details '는 'details()인 함수로 사용 '. 그러므로 우리는 결과를 얻지 못할 것입니다. 대신 괄호를 제거하면 키워드 'get ' 속성 "details"를 함수가 아닌 속성으로 처리해야 합니다.
<html>
<body>
<p id = "prop"></p>
<script>
var business = {
product: "Tutorix",
parent : "Tutorialspoint",
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 : "Tutorialspoint",
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 Tutorialspoint