什么是原型和原型链

参考:https://blog.csdn.net/xiaoermingn/article/details/80745117

什么是原型和原型链

一、原型

  • JS 中所有 引用类型数据 都有一个__proto__(隐式原型)属性,属性值是一个对象;
  • 引用类型 __proto__属性 指向它的构造函数的prototype
  • 所有函数都有一个 prototype(原型)属性,属性值是一个对象;

二、原型链

任何一个 引入型数据 都有一个原型属性(prototype),当外部获取某个属性的时候,先获取当前引入型数据中的属性,如果没有进入原型(prototype)中寻找,如果还是没有继续到下一层原型中找,直到找到Object原型为止。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>原型链继承和什么是原型链</title>
</head>
<body>

<script>
  // ES5 继承 prototype
  function User(username, password){
    this.username = username;
    this.password = password
    this.login = function(){
      console.log('登录')
    }
  }

  function Admin(){
    this.deletPerson = function(){
      console.log('删除一个人')
    }
  }

  Admin.prototype = new User()

  let admmin = new Admin()

  admmin.login()

  /**
   * 什么是原型链
   * 任何一个引入型数据都有一个原型属性(prototype),
   * 当外部获取某个属性的时候,先获取当前引入型数据中的属性,
   * 如果没有进入原型(prototype)中寻找,如果还是没有继续到下一层原型中找,
   * 直到找到Object原型为止。
  */
</script>
</body>
</html>
posted @ 2022-12-06 22:18  轻风细雨_林木木  阅读(38)  评论(0编辑  收藏  举报