Javascript:对象原型__proto__

  • 对象都会有一个属性__proto__,称为对象原型,它指向构造函数的prototype原型对象。
  • 对象之所以可以使用prototype原型对象的属性和方法,就是因为对象有__proto__的存在。
  • 注意__proto__是js的非标准属性,在不同的浏览器中可能显示不同,[[Prototype]]__proto__ 的意义相同。但在代码中,要查看__proto__的话,必须写__proto__,不能写[[Prototype]]
  • __proto__属性是只读的。
  • __proto__对象原型里面也有一个constructor属性,指向创建该实例对象的构造函数。

示例

查看__proto__属性

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function Star() { }
    const star1 = new Star()
    console.log(star1)
  </script>
</body>

</html>

下面[[Prototype]] 指的就是 __proto__
在这里插入图片描述

示例:验证对象原型__proto__ 指向 该构造函数的原型对象prototype

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function Star() { }
    const star1 = new Star()
    console.log(star1.__proto__)
    // 对象原型__proto__ 指向 该构造函数的原型对象prototype
    console.log(star1.__proto__ === Star.prototype)   // 返回true
  </script>
</body>

</html>

在这里插入图片描述

示例:__proto__对象原型里面constructor指向创建该实例对象的构造函数

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function Star() { }
    const star1 = new Star()
    console.log(star1.__proto__.constructor)
  </script>
</body>

</html>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值