JavaScript中constructor和prototype属性
来源:互联网 发布:微信数据库损坏怎么办 编辑:程序博客网 时间:2024/04/27 22:36
JavaScript中,无论什么时候,只要创建了一个函数,就会根据一组特定的规则为该函数创建一个prototype属性,这个属性指向了该函数的原型对象,在默认情况下,所有的原型对象都会自动获得一个constructor属性,该属性是一个指针,指向prototype所在的函数。
我们可以通过constructor找到构造函数,实际上是因为,该对象继承的构造函数的原型对象,通过调用原型对象中的construction指针找到了prototype指针所在的函数,也就是该对象的构造函数。
当我们重写整个原型对象的时候该原型对象就变成了完全不同的另一个对象,幸运的是,我们可以通过直接给constructor赋值来指定该指针的值,但是构造函数创建的对象中的prototype指针却没办法指向新原型了
贴代码:
function Person(){};var friend=new Person();alert(friend.__proto__===Person.prototype); //true 表示friend的__proto__指向的正是构造函数的原型对象var tmp=Person.prototype;alert(friend.__proto__===Person.prototype); //true 先将原生对象保存Person.prototype={ constructor:Person, //强制让原型的constructor指针指向原构造函数。 name:'abc', age:100, job:'def'};alert(friend.name); //undefined alert(friend.__proto__===Person.prototype); //false friend中的__proto__指针指向的不是现在的原型对象alert(friend.__proto__===tmp); //true 说明friend中的__proto__指针指向的是原来的原型对象//__proto__连接存在于实例与构造函数的原型对象之间,而不是实例与构造函数之间
0 0
- JavaScript中constructor和prototype属性
- constructor和prototype属性
- 【转】Javascript中prototype和constructor详解
- JavaScript中几个重要的属性(this、constructor、prototype)
- JavaScript中几个重要的属性(this、constructor、prototype)介绍
- javascript 的prototype 和constructor
- JavaScript的constructor和prototype
- Javascript属性constructor/prototype的底层原理
- JavaScript Object.prototype.constructor属性详解
- JavaScript---对象学习(一)object对象、string对象、javaScript的constructor属性和prototype属性
- Javascript中this、prototype、constructor的理解
- Javascript中this、prototype、constructor的理解
- Javascript中this、prototype、constructor的理解
- Javascript中this、prototype、constructor的理解
- Javascript中this、prototype、constructor的理解
- Javascript中this、prototype、constructor的理解
- Javascript中this、prototype、constructor的理解
- javascript中构造函数和prototype属性
- 关于vertical-align:middle使img标签图片居中的办法
- React Native开源项目-稀土掘金客户端(Android、iOS双适配)
- CVPR2016 Top-push Video-based Person Re-identificantion
- dfs序,时间戳
- LeetCode 404. Sum of Left Leaves
- JavaScript中constructor和prototype属性
- 随机生成1000万个随机数作为海量测试数据
- 网络编程---IO复用之epoll模型
- 存储类、作用域、生命周期、链接属性的关系
- LeetCode 532. K-diff Pairs in an Array
- JVM学习(2)-HotSpot对象
- 使用windows服务+SQL存储过程实现某项信息定时自动发送邮件
- dsadsadsa
- 《重构物联网的未来》读书摘录