随着JavaScript
的发展,JavaScript
的Object
构造函数也增长了许多方法(自身方法和原型链方法。有必要全面了解一下。本文基本涵盖了下Object
全部的属性和方法介绍。若是有遗漏,还请指出。javascript
一些未列出的属性或方法是非标准的、不推荐使用的或者已经从
Web
标准中删除的,本文将再也不进行介绍。html
首先须要介绍一下JavaScript
对象的属性描述符
和对象属性的可枚举性
的概念。java
建立对象的方式有三种:第一种,经过new
操做符后面跟Object
构造函数,第二种,对象字面量方式。第三种,使用Object.create()
方法。以下:es6
const obj = {x: 5,y: 6}
const obj1 = new Object()
obj1.x = 5
obj2.x = 6
const obj2 = Object.create(Object.prototype, {
x: {
value: 5
},
y: {
value: 6
}
})
复制代码
上面这三种方式建立出来的对象是同样的,有相同的属性。可是这些属性内部都有描述其行为的属性描述符
,保存该属性的一些元信息。以下图:数组
若是要得到对象的属性描述符
,能够经过Object.getOwnPropertyDescriptor()
来获取对象中某个属性的属性描述符
。bash
const obj = { x: 5, y: 6 }
const desc = Object.getOwnPropertyDescriptor(obj, 'x')
/*
{
value: 5
writable: true
enumerable: true
configurable: true
}
*/
console.log(desc)
复制代码
对象里目前存在的属性描述符
有两种主要形式:数据属性描述符(data Property Descriptors )
和访问器(accessor Property Descriptors)属性描述符
。函数
属性描述符
特性是为了实现 JavaScript
引擎用的,所以在 JavaScript
中不能直接访问它们。为了表示特性是内部值,该规范把它们放在了两对儿方括号中,例如[[Enumerable]]
。工具
数据属性描述符
有4
个描述其行为的特性:oop
[[Configurable]]
: 表示可否经过delete
删除属性,可否修改属性的特性,或者可否把属性修改成访问器属性描述符
。一旦为false
,就不能再设置它的(value
,writable
,configurable
)。直接在对象上定义的属性默认值为true
。ui
[[Enumerable]]
: 表示可否能经过for-in,Object.keys(),JSON.stringify(),Object.assign()
遍历属性。默认值为true
。
[[Writable]]
: 表示可否修改属性的值。若是为false
,属性的值就不能被重写,只能为只读。默认值为true
。
[[Value]]
: 包含这个属性的值。读取属性值的时候,从这个位置读。写入属性值时,把新值保存在这个位置。默认值是 undefined
。
访问器属性有4
个描述其行为的特性:
[[Configurable]]
: 表示可否经过delete
删除属性,可否修改属性的特性,或者可否把属性修改成数据属性描述符
。一旦为false
,就不能再设置它的(value
,writable
,configurable
)。直接在对象上定义的属性默认值为true
。
[[Enumerable]]
: 表示可否能经过for-in,Object.keys(),JSON.stringify(),Object.assign()
遍历属性。默认值为true
。
[[Get]]
: 在读取属性时调用的函数。默认值为undefined
。
[[Set]]
: 在写入属性时调用的函数。默认值为undefined
。
任何属性描述符
均可以有名为[[Configurable]]
和[[Enumerable]]
的字段。数据属性描述符
含有 [[Writable]]
和 [[Value]]
特性,访问器属性描述符
含有[[Get]]
和[[Set]]
特性。
以下图所示,绿色表明该属性下可存在的特性,红色表明不可存在的特性。
[[Writable]]
、[[Value]]
和[[Get]]
、[[Set]]
之间是互斥关系。由于一个属性,它不能既属于数据属性描述符
,也属于访问器属性描述符
。若是在Object.defineProperty
中同时定义互斥特性会抛出异常。
JavaScript
对象的属性可分为可枚举
和不可枚举
。它是由内部“可枚举”标志(enumerable
)决定的,true
为可枚举,false
为不可枚举。
对于经过直接的赋值和属性初始化的属性,该标识值默认为即为true
,对于经过Object.defineProperty
,Object.create
等定义的属性,该标识值默认为false
。
const obj = { x: 5 }
Object.defineProperty(obj, 'y', {
value: 6
})
const desc1 = Object.getOwnPropertyDescriptor(obj, 'x')
// {value: 5, writable: true, enumerable: true, configurable: true}
console.log(desc1)
const desc2 = Object.getOwnPropertyDescriptor(obj, 'y')
// {value: 6, writable: false, enumerable: false, configurable: false}
console.log(desc2)
复制代码
可枚举的属性能够经过for...in
循环进行遍历(除非该属性名是一个 Symbol
)。
Object
构造函数属性Object.prototype
JavaScript
规定,每一个函数都有一个prototype
属性,指向一个对象。因此Object
构造函数也会有一个prototype
属性。几乎全部对象都继承了Object.prototype
的属性。这就是全部对象都有valueOf
和toString
方法的缘由,由于这是从Object.prototype
继承的。
Object.length
函数的 length
获得的是形参个数。因此Object.length
的形参个数是1
。
function test1(a, b, c) { }
// 3
console.log(test1.length)
function test2(a, b, c, d) { }
// 4
console.log(test2.length)
// 1
console.log(Object.length)
复制代码
Object.name
Object函数的name
属性,返回该函数的函数名。
// Object
console.log(Object.name)
复制代码
Object
构造函数的方法Object.is()
Object.is()
方法接收两个参数,判断两个值是不是相同的值。
传入一个参数或不传参的状况。
Object.is() // true
Object.is(123) // false
Object.is(undefined) // true
Object.is(null) // false
复制代码
严格相等运算符(===
)和Object.is()
的区别。
+0 === -0 // true
NaN === NaN // false
Object.is(+0, -0) // false
Object.is(NaN, NaN) // true
复制代码
绝大多数状况下,Object.is()
的结果与===
运算符是相同的。除了+0,-0
和NaN
这两种状况使用===
和Object.is()
比较返回结果不一样。
Object.assign()
Object.assign
方法用于对象的合并,将源对象(source
)的全部可枚举属性
,复制到目标对象(target
)。
Array.from()
能够传入多个参数:
target
(必填):目标对象。source
:源对象。
Object.assign(target, ...sources)
若是不传参数或者传入null
和undefined
都会抛出异常。
// Uncaught TypeError: Cannot convert undefined or null to object
Object.assign()
Object.assign(null)
Object.assign(undefined)
复制代码
基本用法。
const target = { id: 1 }
const source1 = { name: 'zhangsan' }
const source2 = { age: 26 }
const newObj = Object.assign(target, source1, source2)
// {id: 1, name: "zhangsan", age: 26}
console.log(newObj)
复制代码
若是只传入一个参数,Object.assign会直接返回该参数。
const target = { id: 1, name: '刘德华' }
const newObj = Object.assign(target)
// true
console.log(newObj === target)
复制代码
若是非对象参数出如今源对象的位置,这些参数都会转成对象,若是没法转成对象,就会跳过。若是undefined
和null
不在首参数,就不会报错。
const target = { id: 1, name: '刘德华' }
const newObj = Object.assign(target, 123,true)
// {id: 1, name: "刘德华"}
console.log(newObj)
const target1 = { id: 1, name: '刘德华' }
// 源对象是字符串的话会以数组形式拷贝到目标对象
const newObj1 = Object.assign(target1, '张学友')
// {0: "张", 1: "学", 2: "友", id: 1, name: "刘德华"}
console.log(newObj1)
// 其余值都不会产生效果
let target2 = { id: 1 };
Object.assign(target2, undefined) === target2 // true
Object.assign(target2, null) === target2 // true
Object.assign(target2, NaN) === target2 // true
Object.assign(target2, false) === target2 // true
Object.assign(target2, 10) === target2 // true
复制代码
若是目标对象与源对象有同名属性,或多个源对象有同名属性,后面的属性就会覆盖前面的属性。
const target = { id: 1, name: '刘德华' }
const source1 = { name: '张学友', age: 50 }
const source2 = { age: 26 }
const newObj = Object.assign(target, source1, source2)
// {id: 1, name: "张学友", age: 26}
console.log(newObj)
复制代码
Object.assign
拷贝的属性是有限制的,只拷贝源对象的自身属性
(不拷贝继承属性),也不拷贝不可枚举的属性
(enumerable:false
)。Object.assign
方法是浅拷贝,而不是深拷贝。若是源对象某个属性的值是对象,那么目标对象拷贝获得的是这个对象的引用。
Object.create()
Object.create()
方法会使用指定的原型对象
及属性
去建立一个新的对象。
Object.create()
能够传入2
个参数:
proto
(必填):新建立对象的原型对象,不传会抛出异常。propertiesObject
(可选):要添加到新建立对象的属性描述符
(新添加的属性是其自身的属性,而不是其原型链上的属性)。若是不传参数,传入undefined
或传入基本数据类型会抛出异常。
// Object prototype may only be an Object or null
Object.create(undefined)
Object.create(123)
Object.create('abc')
复制代码
传入null
的状况。
const obj = Object.create(null)
console.log(obj)
复制代码
传入null
的的时候,也就是将null
设置成了新建立对象的原型,这个新对象就不会有原型链了。因此新对象是很是干净的对象。
使用对象自变量和直接使用 new Object()
建立对象。
const obj = {}
console.log(obj)
const obj1 = new Object()
console.log(obj1)
复制代码
从上图能够看出,使用对象自变量和直接使用new Object()
建立对象的时候,都会继承Object
构造函数的原型对象。这也是它们和使用Object.create(null)
建立出新对象的区别。
传入对象的状况。
// 自定义原型对象
const customPropertyObject = { id: 1, name: 'zhangsan' }
const obj = Object.create(customPropertyObject)
console.log(obj);
复制代码
从上图能够看出 customPropertyObject
被定义在了新建立对象的原型对象上,customPropertyObject
又继承了Object
构造函数的原型对象。
也能够直接使用来指定Object.create(Object.prototype)建立出和对象自变量同样的对象。
const obj = Object.create(Object.prototype)
console.log(obj)
复制代码
这和经过 const obj = {}
直接建立对象是同样的效果。
传入传入2
个参数。
const obj = Object.create({ id: 1 }, {
name: {
value: 'zhangsan',
enumerable: true,
configurable: true,
writable: true,
},
age: {
value: 20,
enumerable: false,
configurable: false,
writable: false,
}
})
console.log(obj)
复制代码
从上图能够看到,第二个参数添加的属性是建立出的对象自身的属性。咱们把age
属性设置成了不可删除
,不可枚举
,不可修改
的属性值。
obj.age = 1
delete obj.age
// id
// name
for (let prop in obj) {
console.log(prop);
}
复制代码
从上图能够看出,当修改,删除obj.age
属性的时候是没有效果的,并且也没有办法经过for in
循环出来。
第二个参数传入null
会抛出异常。
// Uncaught TypeError:Cannot convert undefined or null to object
const obj = Object.create(null, null)
复制代码
Object.getPrototypeOf()
Object.getPrototypeOf()
方法返回指定对象的原型,若是没有继承属性,则返回 null
。
const obj = { id: 1, name: 'zhangsan' }
// 获取的是Object的原型对象,这里省略打印...
const propertyObj = Object.getPrototypeOf(obj)
// true
console.log(propertyObj === Object.prototype)
复制代码
function Car() { }
const newCar = new Car()
// true
console.log(Object.getPrototypeOf(newCar) === Car.prototype)
复制代码
// Object的原型对象上没有原型对象了,就返回了null
// null
console.log(Object.getPrototypeOf(Object.prototype));
// null
console.log(Object.getPrototypeOf({}.__proto__));
复制代码
使用Object.create
建立对象,并使用Object.getPrototypeOf()
获取它的原型对象。
const obj = Object.create({ id: 1 })
const propertyObj = Object.getPrototypeOf(obj)
// {id: 1}
console.log(propertyObj)
复制代码
Object.setPrototypeOf()
Object.setPrototypeOf()
方法为指定对象设置原型,返回该指定对象。
Object.setPrototypeOf()
能够传入2
个参数:
obj
(必填):要设置其原型的对象。prototype
(可选):该对象的新原型(一个对象 或 null
)。const son = { id: 1, name: 'zhangsan' }
// 要设置的原型对象
const father = { money: 100 }
const obj = Object.setPrototypeOf(son, father)
console.log(obj)
复制代码
上图能够看出,Object.setPrototypeOf
方法将对象son
的原型,设置为对象father
,所以son
能够共享father
的属性。
const obj = Object.setPrototypeOf({}, null)
// 会返回一个很是干净的空 {} 对象,没有继承任何原型对象。
console.log(obj)
复制代码
若是传入错误参数类型,会抛出异常。
// Uncaught TypeError: Object prototype may only be an Object or null: undefined
const obj = Object.setPrototypeOf(1)
const obj = Object.setPrototypeOf(null)
const obj = Object.setPrototypeOf(undefined)
// 第二个参数:Uncaught TypeError: Object prototype may only be an Object or null
const obj = Object.setPrototypeOf({}, undefined)
复制代码
Object
操做属性描述符的相关方法Object.defineProperty()
Object.defineProperty
方法容许经过属性描述符(数据属性描述符
和访问器属性描述符
),设置或修改一个属性,而后返回修改后的原对象。
Object.setPrototypeOf()
能够传入3
个参数:
obj
(必填):属性所在的对象。prop
(可选):属性名。descriptor
(必填):属性描述符。
Object.defineProperty
设置属性的时候,若是属性不存在,则建立属性。若是属性已经存在,Object.defineProperty
方法至关于更新该属性的属性描述对象
。
若是参数错误或不填,会抛出异常。
// Uncaught TypeError: Object.defineProperty called on non-object
Object.defineProperty()
Object.defineProperty(1)
// Uncaught TypeError: Property description must be an object: undefined
Object.defineProperty({})
// Uncaught TypeError: Property description must be an object: 1
Object.defineProperty({}, 'abc', 1)
复制代码
Object.defineProperty()
定义数据属性描述符const obj = { id: 1 }
const newObj = Object.defineProperty(obj, 'name', {
value: 'zhangsan',
writable: false,
enumerable: true,
configurable: true
})
// true
console.log(obj === newObj)
//{id: 1, name: "zhangsan"}
console.log(obj)
newObj.id = 10
// 不可修改,严格模式下会抛出异常
newObj.name = 'lisi'
//{id: 10, name: "zhangsan"}
console.log(obj)
复制代码
const obj = { id: 1 }
Object.defineProperty(obj, 'name', {
enumerable: true,
configurable: true,
set(newValue) {
console.log('监听对象属性修改--->个人值是:' + newValue)
},
get() {
console.log('获取对象属性');
return '刘德华' //先硬编码
}
})
// 监听对象属性修改--->个人值是:zhangsan
obj.name = 'zhangsan'
// 获取对象属性
//刘德华
console.log(obj.name)
复制代码
上面这个obj.name
赋值或者取值的时候会分别触发 set 和 get 对应的函数。set
和 get
至关于监听函数。
模拟一个访问和设置的默认行为,达到咱们正确的修改数据和访问数据是正确的。
const obj = { id: 1 }
// 内部 this 指向 obj
Object.defineProperty(obj, 'name', {
enumerable: true,
configurable: true,
set(newValue) {
this.name = newValue
},
get() {
return this.name
}
})
// Uncaught RangeError: Maximum call stack size exceeded
obj.name = 'zhangsan'
复制代码
按照上面这么写的话会形成循环引用。obj.name = 'zhangsan'
会触发set
函数,set
函数内部 this.name = newValue
又会触发set
函数。而后无限调用....就抛出异常。须要定义一个新的属性解决问题。
const obj = { id: 1 }
// 内部 this 指向 obj
Object.defineProperty(obj, 'name', {
enumerable: true,
configurable: true,
set(newValue) {
this._name = newValue
},
get() {
return this._name || undefined
}
})
obj.name = 'zhangsan'
复制代码
上面这样作法有一个缺点,就是obj
对象里多了一个_name
属性。
Object.defineProperties
方法容许经过属性描述符
(数据属性描述符
和访问器属性描述符
),定义或修改多个属性,而后返回修改后的原对象。
Object.defineProperties()
能够传入2
个参数:
obj
(必填):属性所在的对象。prop
(可选):属性描述符对象。若是参数错误或不填,会抛出异常。
// Uncaught TypeError: Object.defineProperty called on non-object
Object.defineProperties()
Object.defineProperties(1,{})
// Cannot convert undefined or null to object
Object.defineProperties({})
Object.defineProperties({},null)
复制代码
const obj = { id: 1 }
Object.defineProperties(obj, {
name: { value: 'zhangsan', enumerable: true, writable: true },
age: { value: 20, enumerable: true, writable: true },
})
// {id: 1, name: "zhangsan", age: 20}
console.log(obj)
复制代码
注意:
Object.defineProperty()
和Object.defineProperties()
的属性描述符对象
,它的writable
、configurable
、enumerable
这三个属性的默认值都为false
。
Object.getOwnPropertyDescriptor()
Object.getOwnPropertyDescriptor()
能够取得指定对象上一个自有属性
(非继承属性)的描述符。若是指定的属性存在于对象上,则返回其属性描述符对象
(property descriptor),不然返回 undefined
。
Object.getOwnPropertyDescriptor()
能够传入2
个参数:
obj
(必填):属性所在的对象。prop
(可选):属性名称。若是不传参数,会抛出异常。
// Uncaught TypeError: Cannot convert undefined or null to object
Object.getOwnPropertyDescriptor()
Object.getOwnPropertyDescriptor(null)
Object.getOwnPropertyDescriptor(undefined)
复制代码
const obj = { id: 1, name: 'zhangsan' }
Object.defineProperty(obj, 'name', {
enumerable: true,
configurable: true,
get() {
return 'get'
},
set() {
return 'set'
}
})
const descObj = Object.getOwnPropertyDescriptor(obj, 'id')
// {value: 1, writable: true, enumerable: true, configurable: true}
console.log(descObj)
// get和set是两个函数
// {enumerable: true, configurable: true, get: ƒ, set: ƒ}
const descObj1 = Object.getOwnPropertyDescriptor(obj, 'name')
console.log(descObj1);
// 由于toString是原型链上的对象,因此访问不到
// undefined
const descObj2 = Object.getOwnPropertyDescriptor(obj, 'toString')
console.log(descObj2);
复制代码
Object.getOwnPropertyDescriptors()
Object.getOwnPropertyDescriptors()
方法,返回指定对象全部自身属性
(非继承属性)的属性描述符对象
。若是没有任何自身属性,返回空对象。
Object.getOwnPropertyDescriptors()
能够传入1
个参数:
obj
(必填):任意对象若是不传参数或传入错误参数,会抛出异常。
// Uncaught TypeError: Cannot convert undefined or null to object
Object.getOwnPropertyDescriptors()
Object.getOwnPropertyDescriptors(null)
Object.getOwnPropertyDescriptors(undefined)
复制代码
const obj = {
id: 1,
name: 'zhangsan'
};
const descObj = Object.getOwnPropertyDescriptors(obj)
/* {
id:{
value: 1,
writable: true,
enumerable: true,
configurable: true
},
name: {
value: 1,
writable: true,
enumerable: true,
configurable: true
}
}
*/
console.log(descObj)
复制代码
Object.getOwnPropertyDescriptors()
方法返回一个对象,全部原对象的属性名都是该对象的属性名,对应的属性值就是该属性的属性描述符对象
。
Object.keys()
Object.keys()
方法会返回由一个指定对象自身
(非继承属性)可枚举属性
组成的字符串数组。
const obj = {
id: 1,
name: 'zhangsan'
};
const props = Object.keys(obj)
// ["id", "name"]
console.log(props)
复制代码
Object.keys()
能够传入1
个参数:
obj
(必填):任意对象若是不传参数,会抛出异常。
// Uncaught TypeError: Cannot convert undefined or null to object
Object.keys()
Object.keys(null)
Object.keys(undefined)
复制代码
const obj = {
id: 1,
name: 'zhangsan'
};
const props = Object.keys(obj)
// ["id", "name"]
console.log(props)
const obj1 = { 100: 'a', 2: 'b', 7: 'c' };
// ['2', '7', '100']
console.log(Object.keys(obj1));
复制代码
Object.values()
Object.values()
方法返回由一个指定对象自身
(非继承属性)可枚举属性
值的数组。
Object.values()
能够传入1
个参数:
obj
(必填):任意对象若是不传参数,会抛出异常。
// Uncaught TypeError: Cannot convert undefined or null to object
Object.values()
Object.values(null)
Object.values(undefined)
复制代码
const obj = {
id: 1,
name: 'zhangsan'
}
const newObj = Object.values(obj)
// [1, "zhangsan"]
console.log(newObj)
复制代码
Object.entries()
Object.entries()
方法返回由一个指定对象自身
(非继承属性)可枚举属性
的键值对数组。
Object.entries()
能够传入1
个参数:
obj
(必填):任意对象若是不传参数,会抛出异常。
// Uncaught TypeError: Cannot convert undefined or null to object
Object.entries()
Object.entries(null)
Object.entries(undefined)
复制代码
const obj = {
id: 1,
name: 'zhangsan'
}
const newObj = Object.entries(obj)
// [['id',1],['name','zhangsan']]
console.log(newObj)
for (let [key, value] of Object.entries(obj)) {
// id: 1
// name: zhangsan
console.log(`${key}: ${value}`);
}
复制代码
Object.fromEntries()
Object.fromEntries()
方法是Object.entries()
的逆操做,用于将一个键值对数组转为对象。
Object.fromEntries()
能够传入1
个参数:
iterable
(必填):可迭代对象,相似 Array
、Map
或者其它可迭代的对象。若是不传参数或传入参数错误,会抛出异常。
// Uncaught TypeError: undefined is not iterable
Object.fromEntries()
Object.fromEntries(undefined)
Object.fromEntries(null)
// Uncaught TypeError: function is not iterable
Object.fromEntries(() => {})
// Uncaught TypeError: Iterator value a is not an entry object
Object.fromEntries('abc')
复制代码
const map = new Map([['id', 1], ['name', 'zhangsan']])
const obj = Object.fromEntries(map)
// {id: 1, name: "zhangsan"}
console.log(obj)
const obj1 = Object.fromEntries([['id', 1], ['name', 'zhangsan']])
// {id: 1, name: "zhangsan"}
console.log(obj1)
复制代码
Object.getOwnPropertyNames()
Object.getOwnPropertyNames()
方法返回一个由指定对象的全部自身属性
(非继承属性)的属性名(包括不可枚举属性
但不包括Symbol值
做为名称的属性)组成的字符串数组。
Object.getOwnPropertyNames()
能够传入1
个参数:
obj
(必填):任意对象。若是不传参数或传入参数错误,会抛出异常。
// Uncaught TypeError: Cannot convert undefined or null to object
Object.getOwnPropertyNames()
Object.getOwnPropertyNames(undefined)
Object.getOwnPropertyNames(null)
复制代码
const obj = { id: 1, name: 'zhangsan' }
const arr = Object.getOwnPropertyNames(obj)
// ["id", "name"]
console.log(arr)
复制代码
Object.getOwnPropertySymbols()
Object.getOwnPropertySymbols()
方法返回一个指定对象自身的全部Symbol
属性的数组。全部的对象在初始化的时候不会包含任何的Symbol
,除非你在对象上赋值了Symbol
不然Object.getOwnPropertySymbols()
只会返回一个空的数组。
Object.getOwnPropertySymbols()
能够传入1
个参数:
obj
(必填):任意对象。若是不传参数或传入参数错误,会抛出异常。
// Uncaught TypeError: Cannot convert undefined or null to object
Object.getOwnPropertySymbols()
Object.getOwnPropertySymbols(undefined)
Object.getOwnPropertySymbols(null)
复制代码
const obj = { id: 1, name: 'zhangsan' }
obj[Symbol('age')] = 20
const arr = Object.getOwnPropertySymbols(obj)
// [Symbol(age)]
console.log(arr);
复制代码
Object
对象的防篡改相关方法Object.preventExtensions()
Object.preventExtensions()
方法让一个对象变的不可扩展,永远不能再添加新的属性。返回已经不可扩展的原对象。
Object.preventExtensions()
能够传入1
个参数:
const obj = { id: 1, name: 'zhangsan' }
const newObj = Object.preventExtensions(obj)
// 在严格模式下,会抛出异常。
obj.age = 20
// {id: 1, name: "zhangsan"}
console.log(obj)
// true
console.log(obj === newObj)
复制代码
Object.isExtensible()
Object.isExtensible()
方法判断一个对象是不是可扩展的(是否能够在它上面添加新的属性)。返回true
是可扩展的。
Object.isExtensible()
能够传入1
个参数:
obj
:任意对象。const obj = { id: 1, name: 'zhangsan' }
// true
console.log(Object.isExtensible(obj))
Object.preventExtensions(obj)
// false
console.log(Object.isExtensible(obj))
复制代码
Object.seal()
Object.seal()
方法封闭一个对象,阻止添加新属性并将全部现有属性标记为不可配置([[Configurable]]
设置为false
)。当前属性的值只要封闭以前是可写的就仍是可写的。返回被密封的的原对象。
Object.seal()
能够传入1
个参数:
obj
:任意对象。const obj = { id: 1, name: 'zhangsan' }
Object.seal(obj)
// 不能新增, 严格模式下抛出异常
obj.age = 20
// 不能删除,严格模式下抛出异常
delete obj.id
复制代码
Object.isSealed()
Object.isSealed()
方法判断一个对象是否被密封。返回true
是被密封的。
Object.isSealed()
能够传入1
个参数:
obj
:任意对象。Object.freeze()
Object.freeze()
方法冻结一个对象。一个被冻结的对象不再能被修改。冻结了一个对象则不能向这个对象添加新的属性,不能删除已有属性,不能修改已有属性的值。也不能修改该对象已有属性的可枚举性、可配置性、可写性,以及。而且冻结一个对象后该对象的原型也不能被修改。freeze()
返回原对象。
Object.freeze()
能够传入1
个参数:
obj
:任意对象。const obj = { id: 1, name: 'zhangsan',house: { 'beijing': 2 } }
Object.freeze(obj)
// 不能新增属性, 严格模式下抛出异常
obj.age = 20
// 不能删除属性,严格模式下抛出异常
delete obj.id
// 不能修改属性,严格模式下抛出异常
obj.id = 20
// 不能修改原型链,严格模式下抛出异常
obj.__proto__ = {}
// 不能修改属性描述符,严格模式下抛出异常
Object.defineProperty(obj, 'id', {
enumerable: false
})
// 若是一个属性的值是个对象,则这个对象中的属性是能够修改的,除非它也是个冻结对象。
obj.house.beijing = 3
// 3
console.log(obj.house.beijing)
复制代码
被冻结对象自身的全部属性几乎都不可能以任何方式被修改。
Object.isFrozen()
Object.isFrozen()
方法判断一个对象是否被冻结。返回true
是被冻结的。
Object.isFrozen()
能够传入1
个参数:
obj
:任意对象。const obj = { id: 1, name: 'zhangsan' }
Object.freeze(obj)
// true
console.log(Object.isFrozen(obj))
复制代码
Object
实例对象(Object
原型)的属性和方法Object.prototype.constructor
prototype
对象有一个constructor
属性,默认指向prototype
对象所在的构造函数。由于Object
是一个函数,因此它的原型对象上的constructor
属性就是Function
。
// true
console.log(Object.__proto__.constructor === Function)
复制代码
Object.prototype.hasOwnProperty()
对象实例的hasOwnProperty
方法返回一个布尔值,用于判断某个属性定义在对象自身,仍是定义在原型链上。
Object.prototype.hasOwnProperty()
能够传入1
个参数:
obj
:要检测的属性的字符串名称,或者 Symbol
。const obj = { id: 1, name: 'zhangsan' }
// true
console.log(obj.hasOwnProperty('id'))
// false
console.log(obj.hasOwnProperty('toString'))
复制代码
Object.prototype.isPrototypeOf()
对象实例的isPrototypeOf()
方法用来判断该对象是否为参数对象的原型。
Object.prototype.isPrototypeOf()
能够传入1
个参数:
obj
:在该对象的原型链上搜索var father = { money: 100 };
var son = Object.create(father);
// true 儿子的原型是爸爸
console.log(father.isPrototypeOf(son))
//因为Object.prototype处于原型链的最顶端,因此对各类实例都返回true,只有直接继承自null的对象除外
Object.prototype.isPrototypeOf({}) // true
Object.prototype.isPrototypeOf([]) // true
Object.prototype.isPrototypeOf(/xyz/) // true
Object.prototype.isPrototypeOf(Object.create(null)) // false
复制代码
Object.prototype.propertyIsEnumerable()
对象实例的propertyIsEnumerable()
方法返回一个布尔值,表示指定的属性是否可枚举。
Object.prototype.对象实例的propertyIsEnumerable()
能够传入1
个参数:
prop
:属性名const obj = { id: 1, name: 'zhangsan' }
Object.defineProperty(obj, 'age', {
enumerable: false
})
// true
console.log(obj.propertyIsEnumerable('id'))
// false
console.log(obj.propertyIsEnumerable('age'))
// false
console.log(obj.propertyIsEnumerable('toString'))
复制代码
Object.prototype.toLocaleString()
对象实例的toLocaleString()
方法返回一个该对象的字符串表示。Object toLocaleString
返回的是调用 toString()
的结果。
const obj = { id: 1, name: 'zhangsan' }
// [object Object]
console.log(obj.toLocaleString())
复制代码
Object.prototype.toString()
对象实例的toString()
方法返回一个表示该对象的字符串。
toString()
检测对象类型。
var toString = Object.prototype.toString;
console.log(toString.call(new Date())) // [object Date]
console.log(toString.call({})) // [object Object]
console.log(toString.call('')) // [object String]
复制代码
Object.prototype.valueOf()
对象实例的 valueOf()
方法返回指定对象的原始值。
const emptyObject = {}
// true
console.log(emptyObject.valueOf() === emptyObject)
const arr = []
// true
console.log(arr.valueOf() === arr)
复制代码
developer.mozilla.org/zh-CN/docs/…
es6.ruanyifeng.com/#docs/objec…