Computer >> Máy Tính >  >> Lập trình >> Javascript

JavaScript getPrototypeOf với ví dụ

Phương thức getPrototypeOf () được sử dụng để kiểm tra nguyên mẫu của đối tượng do người dùng tạo và thường được sử dụng để so sánh xem hai đối tượng đã cho có cùng nguyên mẫu hay không.

Sau đây là mã cho hàm getPrototypeOf () -

Ví dụ

<!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;
   }
   .sample {
      font-size: 18px;
      font-weight: 500;
   }
</style>
</head>
<body>
<h1>JavaScript getPrototypeOf()</h1>
<div class="sample"></div>
<button class="Btn">CLICK HERE</button>
<h3>
Click on the above button to get the prototype of object test
</h3>
<script>
   let sampleEle = document.querySelector(".sample");
   function fun(name, age) {
      this.name = name;
      this.age = age;
   }
   document.querySelector(".Btn").addEventListener("click", () => {
      let test = new fun("Rohan", 22);
      sampleEle.innerHTML = "The prototype for test object is = " + Object.getPrototypeOf(test);
   });
</script>
</body>
</html>

Đầu ra

JavaScript getPrototypeOf với ví dụ

Khi nhấp vào nút “BẤM VÀO ĐÂY” -

JavaScript getPrototypeOf với ví dụ