Object.keys(obj)
:返回一个由对象的所有可枚举属性组成的数组。
const obj = { a : 1 , b : 2 , c : 3 } ;
console. log ( Object. keys ( obj) ) ;
Object.values(obj)
:返回一个由对象的所有可枚举属性值组成的数组。
const obj = { a : 1 , b : 2 , c : 3 } ;
console. log ( Object. values ( obj) ) ;
Object.entries(obj)
:返回一个由对象的所有可枚举属性键值对组成的数组。
const obj = { a : 1 , b : 2 , c : 3 } ;
console. log ( Object. entries ( obj) ) ;
Object.assign(target, ...sources)
:将一个或多个源对象的所有可枚举属性复制到目标对象中,并返回目标对象。
const target = { a : 1 , b : 2 } ;
const source = { b : 3 , c : 4 } ;
const result = Object. assign ( target, source) ;
console. log ( result) ;
Object.freeze(obj)
:冻结一个对象,使其不可修改。
const obj = { a : 1 , b : 2 } ;
Object. freeze ( obj) ;
obj. a = 3 ;
console. log ( obj) ;
Object.seal(obj)
:封闭一个对象,使其不可添加或删除属性,但可以修改属性值。
const obj = { a : 1 , b : 2 } ;
Object. seal ( obj) ;
obj. a = 3 ;
obj. c = 4 ;
delete obj. b;
console. log ( obj) ;
Object.hasOwnProperty()
:判断一个对象是否包含指定的属性。
const obj = { a : 1 , b : 2 } ;
console. log ( obj. hasOwnProperty ( 'a' ) ) ;
console. log ( obj. hasOwnProperty ( 'c' ) ) ;
Object.create(proto, [propertiesObject])
:使用指定的原型对象创建一个新对象。
const person = {
name : 'John' ,
age : 30
} ;
const student = Object. create ( person, {
grade : {
value : 'A'
}
} ) ;
console. log ( student. name) ;
console. log ( student. grade) ;
Object.fromEntries(iterable)
:将一个键值对数组转换为一个对象。
const entries = [ [ 'name' , 'John' ] , [ 'age' , 30 ] ] ;
const person = Object. fromEntries ( entries) ;
console. log ( person) ;
Object.defineProperty(obj, prop, descriptor)
:定义一个新属性或修改一个已有属性的特性
。
const person = { } ;
Object. defineProperty ( person, 'name' , {
value : 'John' ,
writable : false
} ) ;
person. name = 'Mike' ;
Object.defineProperties(obj, props)
:定义一个或多个新属性或修改已有属性的特性
。
const person = { } ;
Object. defineProperties ( person, {
name : {
value : 'John' ,
writable : false
} ,
age : {
value : 30 ,
writable : true
}
} ) ;
person. name = 'Mike' ;
person. age = 31 ;
console. log ( person. age) ;
Object.getOwnPropertyDescriptor(obj, prop)
:返回指定属性的描述符
。
const person = { } ;
Object. defineProperty ( person, 'name' , {
value : 'John' ,
writable : false
} ) ;
const descriptor = Object. getOwnPropertyDescriptor ( person, 'name' ) ;
console. log ( descriptor) ;
Object.getOwnPropertyDescriptors(obj)
:返回指定对象所有属性的描述符
。
const person = { } ;
Object. defineProperties ( person, {
name : {
value : 'John' ,
writable : false
} ,
age : {
value : 30 ,
writable : true
}
} ) ;
const descriptors = Object. getOwnPropertyDescriptors ( person) ;
console. log ( descriptors) ;
Object.getOwnPropertyNames(obj)
:返回一个数组,包含指定对象自身所有属性的名称
。
const person = {
name : 'John' ,
age : 30
} ;
const names = Object. getOwnPropertyNames ( person) ;
console. log ( names) ;
Object.getPrototypeOf(obj)
:返回指定对象的原型
。
const person = { } ;
const student = Object. create ( person) ;
console. log ( Object. getPrototypeOf ( student) === person) ;
Object.is(value1, value2)
:比较两个值是否相等。
console. log ( Object. is ( 1 , 1 ) ) ;
console. log ( Object. is ( 1 , '1' ) ) ;
console. log ( Object. is ( NaN , NaN ) ) ;
Object.setPrototypeOf(obj, proto)
:设置一个对象的原型
。
const person = { } ;
const student = { } ;
Object. setPrototypeOf ( student, person) ;
console. log ( Object. getPrototypeOf ( student) === person) ;
Object.toString()
:返回对象的字符串表示。
const person = {
name : 'John' ,
age : 30
} ;
console. log ( person. toString ( ) ) ;