首页IT科技javascript 原型,原型链 ? 有什么特点?(记录–JavaScript原型和原型链复习笔记)

javascript 原型,原型链 ? 有什么特点?(记录–JavaScript原型和原型链复习笔记)

时间2025-07-16 03:14:40分类IT科技浏览5157
导读:这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助...

这里给大家分享我在网上总结出来的一些知识             ,希望对大家有所帮助

对于引用类型来说 constructor 属性值是可以修改的                    ,但是对于基本类型来说是只读的,因为创建他们的是只读的原生构造函数(native constructors)

2. 原型链

每个对象拥有一个原型对象      ,通过 __proto__ 指针指向上一个原型       ,并从中继承方法和属性                    ,同时原型对象也可能拥有原型             ,这样一层一层      ,最终指向 null             。这种关系被称为原型链 (prototype chain)                   ,通过原型链一个对象会拥有定义在其他对象中的属性和方法                    。

因此             ,当读取实例的属性时,如果找不到                   ,就会查找与对象关联的原型中的属性                   ,如果还查不到,就去找原型的原型             ,一直找到最顶层为止      。

2.1 原型链知识点

原型链的尽头(root)是Object.prototype      。所有对象(除null)均从Object.prototype继承属性

Object.prototype.__proto__值是null                   ,原型链终止

Function.prototype和Function.__proto__为同一对象

意味着: Object/Array/String等等构造函数本质上和Function一样      ,均继承于Function.prototype

Function.prototype直接继承root(Object.prototype)

继承的原型链:Object.prototype(root)<---Function.prototype<---Function|Object|Array...

对象的__proto__指向自己构造函数的prototype

ES规范定义对象字面量({})的原型就是Object.prototype

2.2 Object和Function的鸡和蛋的问题

Function.prototype是个不同于一般函数(对象)的函数(对象) Function.prototype像普通函数一样可以调用             ,但总是返回undefined                    。 普通函数实际上是Function的实例                    ,即普通函数继承于Function.prototype             。func.__proto__ === Function.prototype      。 Function.prototype继承于Object.prototype      ,并且没有prototype这个属性                   。func.prototype是普通对象      ,Function.prototype.prototype是null             。 总结                    ,Function.prototype其实是个另类的函数             ,可以独立于/先于Function产生。 Object本身是个(构造)函数      ,是Function的实例                   ,即Object.__proto__就是Function.prototype

问题总结:

先有Object.prototype(原型链顶端)             ,Function.prototype继承Object.prototype而产生,最后                   ,Function和Object和其它构造函数继承Function.prototype而产生

2.3 原型链图解

原型和原型链经典关系图
自己画的原型图

图解描述:

Person             、Object                   、Function是函数对象                   ,具备prototype属性,其他对象是只有__proro__

获取原型对象

Person.__proto__及Object.__proto__与Function.__proto__相等             ,是ƒ () { [native code] }

Person.__proto__及Object.__proto__与Function.__proto__的原型对象为Object.prototype

Function.__proto__和Function.prototype值相等                   ,为空函数: ƒ () { [native code] }

Person.constructor       、Object.constructor与Function值相等      ,为: ƒ Function() { [native code] }

原型链代码输出结果
function Person(name) { this.name = name } var p2 = new Person(king); console.log(p2.__proto__) //Person.prototype console.log(p2.__proto__.__proto__) //Object.prototype console.log(p2.__proto__.__proto__.__proto__) // null console.log(p2.__proto__.__proto__.__proto__.__proto__) //null后面没有了             ,报错 console.log(p2.__proto__.__proto__.__proto__.__proto__.__proto__) //null后面没有了                    ,报错 console.log(p2.constructor)//Person console.log(p2.prototype)//undefined p2是实例      ,没有prototype属性 console.log(Person.constructor)//Function 一个空函数 console.log(Person.prototype) //打印出Person.prototype这个对象里所有的方法和属性 console.log(Person.prototype.constructor)//Person console.log(Person.prototype.__proto__)// Object.prototype console.log(Person.__proto__) //Function.prototype console.log(Function.prototype.__proto__)//Object.prototype console.log(Function.__proto__)//Function.prototype console.log(Object.__proto__)//Function.prototype console.log(Object.prototype.__proto__)//null console.log(Function); // ƒ Function() { [native code] } 空函数      ,名为Function console.log(Object.constructor); // ƒ Function() { [native code] } console.log(Person.constructor); // ƒ Function() { [native code] } console.log(Function === Object.constructor); // true console.log(Function === Person.constructor); // true console.log(Function.__proto__); // ƒ () { [native code] } console.log(Function.prototype); // ƒ () { [native code] } console.log(Function.__proto__ == Function.prototype); // true console.log(Function.__proto__.__proto__ === Object.prototype) // true console.log(Function.prototype.__proto__ === Object.prototype) // true

本文转载于:

https://juejin.cn/post/7086376102238617614

如果对您有所帮助                    ,欢迎您点个关注             ,我会定时更新技术文档      ,大家一起讨论学习                   ,一起进步                   。

声明:本站所有文章             ,如无特殊说明或标注,均为本站原创发布                   。任何个人或组织                   ,在未征得本站同意时                   ,禁止复制             、盗用                   、采集       、发布本站内容到任何网站      、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理             。

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
spring security自定义登录接口(Spring Security 自定义登录Session配置) vps默认端口(vps划分多端口的方法是什么)