클래스
클래스 함수의 유형이지만 'function 키워드를 사용하는 대신 ', 키워드 '클래스 '는 이를 시작하는 데 사용되며 속성은 constructor() 내부에 할당됩니다. 방법. 생성자() 메소드는 클래스 객체가 초기화될 때마다 호출됩니다.
예시-1
다음 예에서 클래스 '회사라고 함 '가 생성되고 constructor() 내부에 방법 회사 이름을 할당하고 결과를 출력에 표시합니다.
<html>
<body>
<p id="class"></p>
<script>
class Company {
constructor(branch) {
this.name = branch;
}
}
myComp = new Company("Tutorialspoint");
document.getElementById("class").innerHTML = myComp.name;
</script>
</body>
</html> 출력
Tutorialspoint
예시-2
<html>
<body>
<script>
class Company {
constructor(branch) {
this.name = branch;
}
}
myComp = new Company("Rk enterprises");
document.write(myComp.name);
</script>
</body>
</html> 출력
Rk enterprises