day25Object方法

概述:Object是所有类的父类,它的属性和方法所有对象可以使用。object的相关方法和属性是提供给其他的对象使用的。

相关属性和方法

 

 原型方法及属性(实例方法和属性)

hasOwnProperty判断当前对象是否具备这个属性  不会读取原型

let obj = {name:'jack'}
function Person(name,age){
this.name = name
this.age = age
}
Person.prototype.sex = '男'
let person = new Person('tom',18)
person.address = '湖南长沙'
//hasOwnProperty 判断当前对象是否具备属性的 返回的值为布尔类型 传入对应需要判断的属性名
console.log( obj.hasOwnProperty('name'));//true
console.log( person.hasOwnProperty('age'));//true
//hasOwnProperty不会读取原型的属性
console.log( person.hasOwnProperty('sex'));//false
console.log(person.hasOwnProperty('address'));//true

 

isPrototypeOf判断当前对象是否在传入对象的原型链上

//isPrototypeOf 是否对象存在于原型链上
//组合继承
function Son(){
Person.call(this)
}
let person1 = new Person('tom',18)
Son.prototype = person1
//组合继承
function Child(){
Son.call(this)
}
let son = new Son()
Child.prototype = son
let child = new Child()
//person1是否处在对应son的原型(__proto__)上
//判断当前对象是否处在传入对应对象的__proto__(原型链上)上
console.log(person1.isPrototypeOf(son));//true
console.log(son.isPrototypeOf(child));//true
console.log(person1.isPrototypeOf(child));//true

 

propertyIsEnumerable判断当前属性是否可枚举

toLocaleString根据本地格式转为字符串

toString转为字符串

Object.prototylpe.toString.call(需要查找的变量,(v)=>{这个v里面就有对应的类型})

valueOf提取对应的value值

原型属性

constructor指向当前对象的构造函数

--proto--对象隐式原型

静态方法(Object.方法调用的)

Object.is判断两个对象是否是一个对象  Object.is(NaN,NaN)//true

Object.assign将后面的对象拷贝到第一个对象中(浅拷贝)

let obj = {arr:[1,2,3]}
//将后面对象的内容拷贝到前面这个对象里面返回前面这个对象
let obj1 = Object.assign(obj,{name:'jack'},{age:18})
console.log(obj);
console.log(obj1 == obj);//true
//对应的浅拷贝完成 (浅拷贝只拷贝第一层的值 第二层拷贝的都是地址)
let copyObj = Object.assign({},obj)
console.log(copyObj);
console.log(copyObj == obj);//false
copyObj.arr.push(4)
console.log(obj.arr);

creat 根据传入的内容创建相关的对象

//create方法
function Person(name){
this.name = name
}
let person = new Person('tom')
//根据传入对象创建相关的对象 但是这个对象跟传入的对象不是一个对象
//它会将创建对象的原型指向传入对象
let createObj = Object.create(person)
console.log(createObj);
console.log(person == createObj);//false
console.log(person.isPrototypeOf(createObj));//true

keys将他的可枚举的key全部加入到一个迭代器返回

values将它的value全部加入到一个迭代器返回

entries将它的键值对全部加入到一个迭代器返回

getPrototypeOf 获取原型

setPrototypeOf设置原型

//setPrototypeOf 设置原型
function Child(name){
this.name = name
}
let child = new Child('tom')
let proto = {address:'北京',city:"河北石家庄"}
Object.setPrototypeOf(child,proto)
console.log(child);
//getPrototypeOf 获取原型
let protoObj = Object.getPrototypeOf(child)
console.log(protoObj);

freeze 冻结(只能查询) isFrozen 是否冻结
seal 密封 (只能查询和修改 不能删除和添加新的属性)isSealed 是否密封
preventExtensions 不可扩展 (不能添加新的属性) isExtensible 是否可扩

freeze > seal > preventExtensions (冻结必定密封 密封必定不可扩展)

getOwnPropertyNames 获取所有的属性名(除symbol的key以外)
getOwnPropertySymbols 获取所有symbol为键的属性名
getOwnPropertyDescriptor 获取对应属性的详情(属性对象)
getOwnPropertyDescriptors 获取所有属性的详情 (对象)

let symbol = Symbol()
let obj = {
name:
'
jack',
age:18
}
obj[symbol] = '你好'
//获取所有的属性名(除symbol的key以外) 传入的是一个对象 返回的是一个字符串数组
let names = Object.getOwnPropertyNames(obj)
console.log(names);
//获取所有的symbol修饰的属性(获取所有symbol为键的属性) 返回symbol数组
let symbols = Object.getOwnPropertySymbols(obj)
console.log(symbols);
//Descriptor getOwnPropertyDescriptor 获取属性的详情 返回是一个属性对象
let desc= Object.getOwnPropertyDescriptor(obj,'name')
console.log(desc);
//getOwnPropertyDescriptors 获取所有的属性详情 返回的是一个对象 每个的对象key是对应的属性
名 值为属性对象
let descs = Object.getOwnPropertyDescriptors(obj)
console.log(descs);

属性对象

属性属性(基础的属性定义)

configurable:false,

enumerable:false,

value:18,

writable:false

defineProperty定义单个属性

访问器属性(已经存在的属性进行访问)

configurable:false,

enumerable:false

get(){},//value

set(){}//writable

//defineProperty的基础使用
let obj = {
name:
"
jack"
}
//基础的属性定义 传入对象 传入属性名 属性对象
Object.defineProperty(obj,'age',{
configurable:false,//不写默认为false 是否可以删除
enumerable:false, //是否可枚举 是否可以被遍历
value:18,
writable:false //是否可以写 修改
})
console.log(obj);
delete obj.age //configurable
//是否可以遍历 for in只能遍历可枚举的属性
for(key in obj){
console.log(key);
}
obj.age = 30
console.log(obj);

 

defineProperties定义多个属性

//定义多个属性
let obj1 = {}
let model1 = {}
Object.defineProperties(obj1,{
username:{
configurable:true,
enumerable:true,
get(){
return model1.username
},
set(v){
model1.username = v
}
},
password:{
configurable:true,
enumerable:true,
get(){
return model1.password
},
set(v){
model1.password = v
}
}
})
obj1.username = '张三'
obj1.password = '123456'
console.log(obj1);
console.log(model1);

for in和Object.keys和Object.getOwnPropertyNames的区别

for in包含继承的属性及自身的属性(不包含不可枚举的属性)

Object.keys只包含自身的属性(不包含继承的和不可枚举的属性)

Object.getOwnPropertyNames包含自身的属性和不可枚举的属性(不包含继承的)

三个内容都不包含symbol属性

posted @ 2022-11-03 19:59  xiaomingff  阅读(16)  评论(0)    收藏  举报