정적 방법
정적 방법을 사용하여 클래스의 요소에만 액세스할 수 있지만 개체의 요소에는 액세스할 수 없습니다. 정적 메서드를 호출할 수 있습니다. 클래스 내부에만 있지만 객체에는 없습니다.
예시-1
다음 예에서 static() 메소드가 "Company 클래스에서 시작됨 "myComp" 개체가 아니라 ". 따라서 static() 의 내용 메소드가 출력에서 실행되었습니다.
<html>
<body>
<p id="method"></p>
<script>
class Company {
constructor(branch) {
this.name = branch;
}
static comp() {
return "Tutorix is the best e-learning platform"
}
}
myComp = new Company("Tesla");
document.getElementById("method").innerHTML = Company.comp();
</script>
</body>
</html> 출력
Tutorix is the best e-learning platform
예시-2
다음 예에서는 class 대신 , 개체가 호출되므로 출력이 실행되지 않습니다. 브라우저 콘솔을 열면 "myComp.comp() "는 함수가 아닙니다.
<html>
<body>
<p id="method"></p>
<script>
class Company {
constructor(branch) {
this.name = branch;
}
static comp() {
return "Tutorix is the best e-learning platform"
}
}
myComp = new Company("Tesla");
document.getElementById("method").innerHTML = myComp.comp();
</script>
</body>
</html>