1.创建对象
1.1对象字面量来创建对象,如下
let book={ title:'js权威指南', author:'xiaoxi', price:30}
1.2 通过new关键字来创建对象
let obj=new Object({name:'jiaojiao'});//它类似于{}
1.3 通过Object.create()创建新对象
Object.create()
他有两个参数,第一个是对象的原型,第二个参数是对对象的属性进行进一步的描述对
let person={ name:'jiaojiao', age:'20'}
let o1 = Object.create(person, { adress: { writable: true, configurable: true, value: "hello" } });
console.log(o1) //
![](https://img.haomeiwen.com/i12533422/6cbc8a544d8351f8.png)
我们可以通过null值创建一个没有原型的新对象
let o2=Object.create(null);
console.log(o2)
![](https://img.haomeiwen.com/i12533422/91cc956e6dff2550.png)
let o3=Object.create(Object.prototype)// 相当于new Object
console.log(o3)
2.原型
每一个js对象(null)除外都和另一个对象关联除了(null)之外。“每一个对象”都是从原型继承属性。
所有通过对象直接创建的对象都有一个原型对象,并可以通过js代码Object.prototype获得对原型对象的引用 通过new Object({name:'jiaojiao'})和{}创建的对象他的原型是Object.prototype,通过new Array()创建的原型是 Array.prototype,通过new Date()创建的原型是Data.prototype
没有原型的对象为数不多,Object.prototype就是其中之一。它不继承任何属性。其他原型对象都是普通对象,普通对象都具有原型。所有的内置构造函数(以及大部分自定义的构造函数)都具有一个继承自Object.prototype,例如Date.prototype的属性继承了Object.prototype,通过new Date()创建的Date对象属性同时继承了Date.prototype和Object.prototype。这一系列的原型对象就是所谓的原型链
3.属性的查询和设置
我们可以通过(.)和方括号的([ ])来获取属性值
let person = { name: 'jiaojiao', age: '20' }
console.log(person.name);
console.log(person['name'])
4.继承
案例一
function inherit(p){
if(p==null){
throw TypeError();
}
return Object.create(p);
let t=typeof p;
if(t!=='object' && t!=='function'){
throw TypeError();
}
function f(){
}
f.prototype=p;
return new f();
}
let o={
};//o继承了Object.prototype
o.x=1;//o自己的属性
let p=inherit(o);//p继承了o和Object.prototype
p.y=2;//p自己的属性
let q=inherit(p);//q继承了p,o ,Object.prototype
q.z=3;//q自己的属性
let s=q.toString();
console.log(q.x+q.y);//3
console.log(q)
案例二
因为c继承了unitcircle的属性,c自己本身也有属性,那么继承的属性就被新创建的同名属性覆盖了,如果允许属性赋值操作,它不会去修改原型链
function inherit(p){
if(p==null){
throw TypeError();
}
return Object.create(p);
let t=typeof p;
if(t!=='object' && t!=='function'){
throw TypeError();
}
function f(){
}
f.prototype=p;
return new f();
}
let unitcircle={r:1};
let c=inherit(unitcircle);
c.x=1;
c.y=1;
c.r=2;
console.log(unitcircle.r);//1 原型对象没有修改
console.log(c);
console.log(c.r);//2
5.删除属性
let book1={
name:'123',
title:'wefrrb',
price:30
}
delete book1.price;
delete book1['title'];
console.log(book1);//{ name: "123"}
6.检测属性
6.1hasOwnProperty()方法检测给定的名字是否是对象自有属性
let o={x:1};
console.log(o.hasOwnProperty('x'))//true
console.log(o.hasOwnProperty('y'))//false
console.log(o.hasOwnProperty('toString'));//false toString是继承属性
6.2 propertyIsEnumerable检测自有属性是否可枚举
let o={x:1};
console.log(o.propertyIsEnumerable('x'));//true
console.log(o.hasOwnProperty('toString'));//false toString是继承属性
6.3 可以用!==判断一个自有属性是否存在
let o={ x:1}
console.log(o.x!==undefined)//true
console.log(o.y!==undefined)//false
7.枚举属性
7-1枚举自有属性
let o={x:1,y:2,z:3};
for(p in o){
console.log(p)
}
console.log(Object.keys(o))//['x', 'y', 'z']
8.object存储器属性getter和setter
当js查询存储性属性值,js调用getter方法,当js设置存储性属性值时,js调用setter方法
let obj = {
count: 1,
get plus() {
console.log('get plus')
return this.count
},
set plus(val) {
console.log('set plus')
this.count += val
}
}
// 控制台:
obj.count // 1
obj.plus // 1
obj.plus = '2' // 3
obj.count // 3
8.属性的特性
8.1.getOwnPropertyDescriptor
//获取某个对象特定属性的属性描述
//对于继承属性和不存在属性返回undefined
let o={
x:1,
y:2,
c:3
}
console.log(Object.getOwnPropertyDescriptor(o,'x'));//true
//{value: 1, writable: true, enumerable: true, configurable: true} value 值 writable可写性 enumerable可枚举性 configurable可配置性
console.log(Object.getOwnPropertyDescriptor(o,'z'));//undefined
console.log(Object.getOwnPropertyDescriptor(o,'toString'));//undefined
8.2 获取原型
Object.getPrototypeOf获取原型
let p={x:1};
let o=Object.create(null);
Object.setPrototypeOf(o,p) ;
console.log(Object.getPrototypeOf(o)); //p
9.序列化对象
序列化对象是指将对象状态转化为字符串,也可将字符串转化为对象JSON.stringify()和JSON.parse()
let o={
x:1,y:{z:[false,null,'']}
}
let s=JSON.stringify(o);
let p=JSON.parse(s);
console.log(s);//'{"x":1,"y":{"z":[false,null,""]}}'
console.log(p);//{x: 1, y: {…}}
10 其他方法
Object.prototype//Object原型
toString()//将对象转换成字符串
{x:1,y:1}.toString()
网友评论