JavaScript getPrototypeOf 示例

javascriptweb developmentobject oriented programming更新于 2025/12/11 5:32:17

getPrototypeOf() 方法用于检查用户创建对象的原型,通常用于比较两个给定对象是否具有相同的原型。

以下是 getPrototypeOf() 函数的代码 −

示例

<!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">点击此处</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>

输出

点击“CLICK HERE”按钮 −


相关文章


有用资源