게터 및 세터 객체 접근자를 정의할 수 있습니다. . 그들 사이의 차이점은 전자는 객체에서 속성을 가져오는 데 사용되는 반면 후자는 객체의 속성을 설정하는 데 사용된다는 것입니다. 사례를 통해 논의해 보겠습니다.
게터
예시
다음 예에서 "business "가 생성되고 "Getter를 사용합니다. "회사라는 속성 "가 출력에 표시됩니다.
<html>
<body>
<script>
var business= {
Name: "Musk",
Country : "America",
Company : "PayPal",
get comp() {
return this.company;
}
};
document.write(business.company);
</script>
</body>
</html> 출력
paypal
세터
예시
다음 예에서 "business "가 생성되고 "Setter를 사용합니다. " "회사라는 속성의 가치 "가 PayPal 에서 변경되었습니다. 솔라시티 로 출력에 표시된 대로.
<html>
<body>
<script>
var business = {
Name: "Musk",
Country : "America",
company : "PayPal",
set comp(val) {
this.company = val;
}
};
business.comp = "SolarCity";
document.write(business.company);
</script>
</body>
</html> 출력
SolarCity