javascript构造函数

作者 : 开心源码 本文共2686个字,预计阅读时间需要7分钟 发布时间: 2022-05-12 共145人阅读

我们相约在今天,在今天探讨javascript构造函数,感谢你如约而至

昨天

我们昨天前几天探讨过构造函数constructor,得出了结论
constructor是原型对象上的一个属性,默认指向这个原型的构造函数
这个结论貌似对我们平常的工作中似乎并没有什么用处,那构造函数,就真的没什么用处吗?

今天

使用构造函数构造可以复用的对象

JS中的函数就可以是构造函数又可以当作普通函数来调用,当使用new来创立对象时,对应的函数就是构造函数,通过对象来调用时就是普通函数。

image
在我们平常工作中,经常会需要我们创立一个对象,而我们更多的是使用对像直接量,直接创立,举个栗子,代码如下

var person = {    name:'postbird',    address:'earth',    sayHello:function(){console.log('Hello,I am ' + this.name);}};

假如只是一个单独的对象,对象的属性和方法基本不会变了,这么玩完全可以,但是假如你的对象有很多实例,或者者涉及继承或者者构造函数传参,留意代码注释

//创立了一个构造函数function Person(name,address){    this.name = name;    this.address = address;}//为构造函数的原型对象增加一个方法sayHelloPerson.prototype.sayHello = function(){    console.log('Hi I am ' + this.name);}//通过构造函数Person实例化一个p1,并传参var p1 = new Person('postbird','earth');//通过构造函数Person实例化一个p2,并传参var p2 = new Person('ptbird','month');console.log(p1);//{name: "postbird", address: "earth"}console.log(p2);//{name: "ptbird", address: "month"}// p1和p2 继承了Person的sayHello方法p1.sayHello()//Hi I am ptbirdp2.sayHello()//Hi I am postbird

耐心档次上面??的代码,这样的可扩展性就会更好,可以创N个实例,实现代码复用

经典案例

关于js的constructor构造函数,有一个很经典的demo

function Person(area){  this.type = 'person';  this.area = area;}Person.prototype.sayArea = function(){  console.log(this.area);}var Father = function(age){  this.age = age;} Father.prototype = new Person('Beijin');console.log(Person.prototype.constructor===Person) //trueconsole.log(Father.prototype.constructor===Person); //trueFather.prototype.constructor = Father;//修正console.log(Father.prototype.constructor===Father); //truevar one = new father(25);console.log(one.constructor===Father) // true

注意这一行代码

Father.prototype.constructor = Father;//修正

为什么要修正?不是说constructor是原型对象上的一个属性,默认指向这个原型的构造函数?
我们把这一行打码注释掉

function Person(area){  this.type = 'person';  this.area = area;}Person.prototype.sayArea = function(){  console.log(this.area);}var Father = function(age){  this.age = age;} Father.prototype = new Person('Beijin');console.log(Person.prototype.constructor===Person) //trueconsole.log(Father.prototype.constructor===Person); //true//Father.prototype.constructor = Father;//修正console.log(Father.prototype.constructor===Father); //falsevar one = new Father(25);console.log(one.constructor===Person) // true

聪明如你,相信你已经发行了问题所在

Father.prototype = new Person('Beijin');

这一步的时候,原型指向了一个新对象,这个新对象的constructor指向的是Person。

console.log((new Person('Beijin')).__proto__ === Person.prototype) //true 

前面我们说过new Person(‘Beijin’)对象是没有prototype的,prototype只有函数才有;
Father.prototype.constructor将会沿着new Person(‘Beijin’)的原型链向下查找constructor,
new Person(‘Beijin’)没有constructor就去它的_proto_找,
由于(new Person(‘Beijin’))._proto_ === Person.prototype
而Person.prototype.constructor == function Person(),
所以 Father.prototype.constructor == Person.prototype.constructor //function Person()
当我们var one = new Father(25) 时 ,one.constructor = Father.prototype.constructor,
所以one.constructor指向function Person(),
所以,肯定要进行修正,否则原型链会乱

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » javascript构造函数

发表回复