실제로 객체 를 사용하려고 하면 결과가 헛된 것입니다. 정적 메서드 내부 . 그러나 객체가 매개변수로 전송되면 객체에 액세스할 수 있습니다. 간단히 설명하겠습니다.
예시-1
다음 예에서는 "myComp " 매개변수로 보내는 대신 직접 , 따라서 우리는 결과를 얻지 못합니다. 브라우저 콘솔을 열면 "myComp.comp()는 함수가 아닙니다. ". 실제 결과를 얻으려면 개체를 매개변수 로 보내야 합니다. 예시-2에 표시된 대로
<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> 예시-2
다음 예에서 객체 매개변수로 전송됩니다. . 따라서 출력에 표시된 대로 얻을 수 있습니다.
<html>
<body>
<p id="method"></p>
<script>
class Company {
constructor(branch) {
this.name = branch;
}
static comp(val) {
return "Elon musk is the head of " + val.name
}
}
myComp = new Company("Tesla");
document.getElementById("method").innerHTML = Company.comp(myComp);
</script>
</body>
</html> 출력
Elon musk is the head of Tesla