本篇旨在梳理对象静态方法,与此同时采用对比的方式介绍Reflect接口,在梳理过程当中当作学习笔记,若是对于读者有帮助就更好了。数组
// 定义三个Symbol类型属性名
let [s1, s2, s3] = [Symbol(), Symbol(), Symbol()]
let parent = {
k1: 'a',
k2: 'b',
[s1]: 's1',
f1: function() {}
}
let child = {}
//child继承parent
Object.setPrototypeOf(child, parent)
//为child对象添加属性及描述
Object.defineProperties(child, {
k3: {
value: 'c',
enumerable: true
},
k4: {
value: 'd',
enumerable: false
},
[s2]: {
value: 's2',
enumerable: true
},
[s3]: {
value: 's3',
enumerable: false
},
f2: {
value: function() {},
enumerable: true
}
})
//arr承载for...in 的属性名
let arr = []
for (let key in child) {
arr.push(key)
}
arr // ["k3", "f2", "k1", "k2", "f1"]
Object.keys(child) //["k3", "f2"]
Object.values(child)//["c", ƒ]
Object.entries(child)//[["k3", "c"],["f2", ƒ]]
Object.getOwnPropertyNames(child) //["k3", "k4", "f2"]
Object.getOwnPropertySymbols(child) // [Symbol(), Symbol()]
Reflect.ownKeys(child) // ["k3", "k4", "f2", Symbol(), Symbol()]
复制代码
对象模型方法包括获取属性描述及定义属性
bash
注意:Object.defineProperty || Reflect.defineProperty || Object.defineProperties 定义属性时,属性描述没有设置为true的话,所有默认为false。
app
let obj = {}
//添加属性描述
Object.defineProperty(obj, 'a', {
value: "a",
enumnerabl: true
})
//添加属性描述
Reflect.defineProperty(obj, 'b', {
value: "b",
writable: true
})
//添加属性描述
Object.defineProperties(obj, {
'c': {
value: 'c',
configurable: true
}
})
Object.getOwnPropertyDescriptor(obj, 'a')
//{value: "a", writable: false, enumerable: false, configurable: false}
Reflect.getOwnPropertyDescriptor(obj, 'a')
//{value: "a", writable: false, enumerable: false, configurable: false}
Object.getOwnPropertyDescriptors(obj)
/*a: {value: "a", writable: false, enumerable: false, configurable: false}
b: {value: "b", writable: true, enumerable: false, configurable: false}
c: {value: "c", writable: false, enumerable: false, configurable: true}*/复制代码
扩展--密封--冻结:对象控制程度依次加强。
函数
let obj = {
a: 'a',
b: 'b'
}
Object.preventExtensions(obj)
Object.isExtensible(obj) //false复制代码
let obj = {
a: 'a',
b: 'b'
}
Reflect.preventExtensions(obj)
Reflect.isExtensible(obj) //false
复制代码
let obj = {
a: 'a',
b: 'b'
}
Object.seal(obj)
Object.isSealed(obj) //true复制代码
let obj = {
a: 'a',
b: 'b'
}
Object.freeze(obj)
Object.isFrozen(obj) //true复制代码
该节主要介绍实例对象之间的继承以及查看实例对象继承源。学习
proto:新建立对象的原型对象,能够为null。propertiesObject:一个或多个属性的描述。
ui
常常见到有文章用new命令与Object.create方法比较,我的理解new命令是建立够构造函数的实例(Object自己就是个函数,能够当成构造函数使用),而如何 从一个实例对象生成另一个实例对象呢,这个时候Object.create就应运而生了。
const parent = {
print: function() {
console.log("I am from parent")
}
}
const child = Object.create(parent)
child.print() //I am from parent
复制代码
class parent {
constructor(x = 1, y = 2) {
this.x = x;
this.y = y
}
multi() {
console.log(this.x * this.y)
}
}
let child = new parent(3, 4)
let grandson = Object.create(child)
grandson.multi() //12复制代码
我的理解:Object.create()与Object.setPrototypeOf()两种方法的差异在于:Object.create()利用原型对象生成一个新对象,而Object.setPrototype()是为已有对象设置原型对象,在实际使用时,在不添加属性集描述状况下,二者没有差异。
区别:若是参数不是对象,Object.getPrototypeOf会将这个参数转为对象,而后再运行,而Reflect.getPrototypeOf会报错。
let a = { a: 'a' }
let b = Object.setPrototypeOf({}, a)
let c = Object.create(a)
//注意Reflect.setPrototypeOf(obj,proto)的使用
let d = {}
Reflect.setPrototypeOf(d, a)
Object.getPrototypeOf(b) //{a: "a"}
Reflect.getPrototypeOf(c) //{a: "a"}
Reflect.getPrototypeOf(d) //{a: "a"}复制代码
Object.assign方法复制的属性有几个点:非继承、属性可枚举、同名覆盖(前提是可枚举)、Symbol类属性惟一,最重要的是只能浅拷贝。this
let [s1, s2, s3] = [Symbol(), Symbol(), Symbol()]
let parent = {
parent: 'parent',
s1: 's1'
}
let child = Object.create(parent, {
c1: {
value: 'c1',
enumbrable: true
},
c2: {
value: 'c2',
enumerable: false
},
c3: {
value: 'c3',
enumerable: true
},
[s2]: {
value: 's2',
enumerable: true
},
[s3]: {
value: 's3',
enumerable: true
}
})
let c_assign = { c1: 'ca1', c2: 'ca2', [s2]: 'sa2', }
Object.assign(c_assign, child, { d: 'd' })
c_assign //{c1: "ca1", c2: "ca2", c3: "c3", d: "d", Symbol(): "s2", Symbol(): "s3"}复制代码
不设置receiver时,获取target对象的属性。若是target不是对象,报错。
spareceiver的功能能够理解为数据源,将数据源中的属性值做用在target对象部署读取函数的属性上。target对象中存在两个关键词get和this。
code
let o1 = {
a: 1,
b: 2,
get sum() {
return this.a + this.b
}
}
let o2 = {
a: 3,
b: 4
}
Reflect.get(o1, 'a') //1
Reflect.get(o1, 'sum', o2) //7复制代码
若是属性propertyKey存在,则更改属性propertyKey值为value;若是属性不存在,则新增一个属性并赋值。
对象当receiver存在时,能够理解为使用目标对象target中设置赋值函数的属性propertyKey设置receiver中属性值为value。target对象中存在两个关键词set和this。
另外,receiver中属性在赋值函数中不存在时,为receiver对象新增一个属性,并赋值。
//展现效果
let o3 = {
a: 1,
set c(v) {
this.a = v
}
}
let o4 = {
a: 0
}
Reflect.set(o3, 'a', 2) //o3.a=2
Reflect.set(o3, 'b', 3) //o3.b=3
Reflect.set(o3, 'c', 5, o4) //o4.a=五、o3.a=2复制代码
let o3 = {
a: 1,
set c(v) {
this.d = v
}
}
let o4 = {
a: 0
}
Reflect.set(o3, 'c', 5, o4) //o4.d=5复制代码
//Reflect.set在Array上的使用
let arr = ['a', 'b', 'c']
Reflect.set(arr, 2, 'd') //arr=["a", "b", "d"]
Reflect.set(arr, 4, 'd') //arr= ["a", "b", "d", empty, "d"]
Reflect.set(arr, 'length', 2) //arr= ["a", "b"]复制代码
thisArg是函数fn调用时绑定的this对象。
args是调用时传递的类数组的对象参数列表
//为了区分教程上案例
const fn = function(v) {
let arr = Reflect.ownKeys(this)
return arr[v]
}
let obj = {
a: 'a',
b: 'b',
c: 'c'
}
Reflect.apply(fn, obj, [1]) //'b'复制代码
这个方法还有第三个参数,可选,暂时不涉及
function A(a) {
this.b = a
}
let a = Reflect.construct(A, ['aa'])
//a={b:'aa'}复制代码
但愿你们针对文中的内容多多指点,另外对于文章没有提到的方法你们提出来,我也会后续进行更改和补充,谢谢你们。