new 연산자는 생성자 함수가 있는 내장 객체 유형 중 하나의 사용자 정의 객체 유형 인스턴스를 만드는 데 사용됩니다.
다음은 JavaScript의 new 연산자에 대한 코드입니다 -
예시
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.result {
font-size: 20px;
font-weight: 500;
color: blueviolet;
}
</style>
</head>
<body>
<h1>New Operator in JavaScript</h1>
<div class="result"></div>
<br />
<button class="Btn">Click Here</button>
<h3>Click on the above button to create the object person1 from Person
constructor</h3>
<script>
let resEle = document.querySelector(".result");
let BtnEle = document.querySelector(".Btn");
function Person(name, age, designation) {
this.name = name;
this.age = age;
this.designation = designation;
}
let person1 = new Person("Rohan Sharma", 19, "student");
BtnEle.addEventListener("click", () => {
resEle.innerHTML = "person1.name = " + person1.name + "<br>";
resEle.innerHTML += "person1.age = " + person1.age + "<br>";
resEle.innerHTML +=
"person1.designation = " + person1.designation + "<br>";
});
</script>
</body>
</html> 출력

'여기를 클릭' 버튼을 클릭하면 -
