详解js创建对象的几种方式和对象方法
这篇文章是看js红宝书第8章,记的关于对象的笔记(第二篇)。
创建对象的几种模式:
工厂模式:
工厂是函数的意思。工厂模式核心是定义一个返回全新对象的函数。
functiongetObj(name,age){ letobj={} obj.name=name obj.age=age returnobj } letperson1=getObj("cc",31)
缺点:不知道新创建的对象是什么类型
构造函数模式:
通过一个构造函数,得到一个对象实例。
构造函数和工厂模式区别是:
1,构造函数函数体加了this
2,构造函数没有return
3,构造函数调用时,用new关键字
functionCreateObj(name,age){ this.name=name this.age=age } letperson1=newCreateObj("cc",31) console.log(person1) console.log(person1.constructor===CreateObj);//true console.log(person1instanceofCreateObj);//true
关于构造函数的两个问题:
1,构造函数和普通函数唯一区别是调用方式。构造函数要用new关键字。如果不用new,则是往Global对象上加属性。下面例子中,CreateObj方法,往window对象上加了name和age属性
functionCreateObj(name,age){ this.name=name this.age=age } CreateObj('cc',10) console.log(window.name)//'cc'
2,构造函数存在的问题:构造函数体内的方法,每次创建一个实例,都会创建一遍。
person1.sayName()===person2.sayName()//false
解决方法是,将sayName定义在createObj外面。
functionsayName(){ console.log(this.name) } functionCreatePerson(name,age){ this.name=name this.age=age this.sayName=sayName } letperson1=newCreatePerson('joy',31) person1.sayName()
但是,这样会让自定义类型引用的代码不能很好聚在一起。
原型模式:
原理是,每个函数都有一个prototype属性。prototype是一个对象,里面定义的属性和方法会被所有实例共享。
关于原型模式,有两个等式:
functionPerson(){} letperson1=newPerson() console.log(person1.__proto__===Person.prototype)//true console.log(Person.prototype.constructor===Person);//true
关于原型对象的三个方法:isPrototype,getPrototypeof,setPrototypeOf,Object.create()
//isPrototypeOf判断构造函数的原型对象是否是实例的原型对象 functionPerson(){} letperson1=newPerson() console.log(Person.prototype.isPrototypeOf(person1));//true
//获取对象的原型对象 functionPerson(){} letperson1=newPerson() console.log(Object.getPrototypeOf(person1)===Person.prototype);
//将某个对象,设为另一个对象的原型对象 letperson1={name:"cc"} letperson2={age:32} Object.setPrototypeOf(person1,person2) console.log(person1.name,person1.age);//cc32
//以某个对象为原型对象,创建一个新对象 letperson1={name:"cc"} letperson2=Object.create(person1) person2.age=30 console.log(person2.name,person2.age);
当访问一个对象person的name属性时,是按照以下步骤去访问:
1,person上如果有name属性(即便这个属性是null,也会返回null),返回name属性值;没有,继续去原型对象Person.prototype上找
2,如果原型上有name属性,返回原型上name属性值;没有,则返回undefined
判断一个属性是在实例上,还是在原型上,可以用hasOwnProperty
functionPerson(){} Person.prototype.name="cc" letperson1=newPerson() console.log(person1.name)//'cc' console.log(person1.hasOwnProperty("name"));//false
判断一个对象上是否有个某个属性,用in操作符
//对象自身or原型上找到,都返回true functionPerson(){} Person.prototype.name="cc" letperson1=newPerson() console.log("name"inperson1)//true console.log(person1.hasOwnProperty("name"));//false
访问对象的属性的方法:
Object.keys() for...in//继承属性也会遍历出来 Object.getOwnPropertyNames(obj)//会列出可枚举和不可枚举属性,其他和Object.keys()结果一样 Object.getOwnPropertySymbols(obj)//和getOwnPropertyNames类似,只是仅针对symbol Reflect.ownKeys(obj)//和Object.keys()结果一样
其他访问对象属性和属性值的方法:
Object.values()对象值组成的数组,会省掉Symbol型。
Object.entries()对象键值对组成的数组,会将键,转化成字符串,省掉Symbol型。
functionPerson(){} Person.prototype.name="cc" letperson=newPerson() person.age=21 letsy=Symbol('sy') person[sy]='smile' console.log(Object.values(person))//[21] console.log(Object.entries(person))//[['age',21]]
到此这篇关于详解js创建对象的几种方式和对象方法的文章就介绍到这了,更多相关js创建对象内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。