目錄
Reflect.get(target, name, receiver)
Reflect.set(target, name, value, receiver)
Reflect.deleteProperty(obj, name)
Reflect.construct(target, args)
Reflect.setPrototypeOf(obj, newProto)
Reflect.apply(func, thisArg, args)
Reflect.defineProperty(target, propertyKey, attributes)
Reflect.getOwnPropertyDescriptor(target, propertyKey)
Reflect.preventExtensions(target)
Reflect對象是ES6 爲了操作對象而提供的新 API。
Reflect.get(target, name, receiver)
1、Reflect.get
方法查找並返回target
對象的name
屬性,如果沒有該屬性,則返回undefined
。
2、如果name
屬性部署了讀取函數(getter),則讀取函數的this
綁定receiver
var myObject = {
foo: 1,
bar: 2,
get baz() {
return this.foo + this.bar;
},
};
var myReceiverObject = {
foo: 4,
bar: 4,
};
Reflect.get(myObject, 'baz', myReceiverObject) // 8
3、
如果第一個參數不是對象,Reflect.get
方法會報錯。
Reflect.set(target, name, value, receiver)
1、Reflect.set
方法設置target
對象的name
屬性等於value
。
2、如果name
屬性設置了賦值函數,則賦值函數的this
綁定receiver
。
var myObject = {
foo: 4,
set bar(value) {
return this.foo = value;
},
};
var myReceiverObject = {
foo: 0,
};
Reflect.set(myObject, 'bar', 1, myReceiverObject);
myObject.foo // 4
myReceiverObject.foo // 1
3、如果 Proxy 對象和 Reflect 對象聯合使用,前者攔截賦值操作,後者完成賦值的默認行爲,而且傳入了receiver
,那麼Reflect.set
會觸發Proxy.defineProperty
攔截。
4、如果第一個參數不是對象,Reflect.set
會報錯。
Reflect.has(obj, name)
1、Reflect.has
方法對應name in obj
裏面的in
運算符。
2、如果第一個參數不是對象,Reflect.has
和in
運算符都會報錯。
Reflect.deleteProperty(obj, name)
1、Reflect.deleteProperty
方法等同於delete obj[name]
,用於刪除對象的屬性。
2、該方法返回一個布爾值。如果刪除成功,或者被刪除的屬性不存在,返回true
;刪除失敗,被刪除的屬性依然存在,返回false
。
Reflect.construct(target, args)
Reflect.construct
方法等同於new target(...args)
,這提供了一種不使用new
,來調用構造函數的方法。
function Greeting(name) {
this.name = name;
}
// new 的寫法
const instance = new Greeting('張三');
// Reflect.construct 的寫法
const instance = Reflect.construct(Greeting, ['張三']);
Reflect.getPrototypeOf(obj)
1、Reflect.getPrototypeOf
方法用於讀取對象的__proto__
屬性,對應Object.getPrototypeOf(obj)
。
2、如果參數不是對象,Object.getPrototypeOf
會將這個參數轉爲對象,然後再運行,而Reflect.getPrototypeOf
會報錯。
Reflect.setPrototypeOf(obj, newProto)
1、Reflect.setPrototypeOf
方法用於設置目標對象的原型(prototype),對應Object.setPrototypeOf(obj, newProto)
方法。它返回一個布爾值,表示是否設置成功。
2、如果無法設置目標對象的原型(比如,目標對象禁止擴展),Reflect.setPrototypeOf
方法返回false
。
3、如果第一個參數不是對象,Object.setPrototypeOf
會返回第一個參數本身,而Reflect.setPrototypeOf
會報錯。
4、如果第一個參數是undefined
或null
,Object.setPrototypeOf
和Reflect.setPrototypeOf
都會報錯。
Reflect.apply(func, thisArg, args)
Reflect.apply
方法等同於Function.prototype.apply.call(func, thisArg, args)
,用於綁定this
對象後執行給定函數。
const ages = [11, 33, 12, 54, 18, 96];
// 舊寫法
const youngest = Math.min.apply(Math, ages);
const oldest = Math.max.apply(Math, ages);
const type = Object.prototype.toString.call(youngest);
// 新寫法
const youngest = Reflect.apply(Math.min, Math, ages);
const oldest = Reflect.apply(Math.max, Math, ages);
const type = Reflect.apply(Object.prototype.toString, youngest, []);
Reflect.defineProperty(target, propertyKey, attributes)
1、Reflect.defineProperty
方法基本等同於Object.defineProperty
,用來爲對象定義屬性
2、如果Reflect.defineProperty
的第一個參數不是對象,就會拋出錯誤
Reflect.getOwnPropertyDescriptor(target, propertyKey)
1、Reflect.getOwnPropertyDescriptor
基本等同於Object.getOwnPropertyDescriptor
,用於得到指定屬性的描述對象
2、如果第一個參數不是對象,Object.getOwnPropertyDescriptor(1, 'foo')
不報錯,返回undefined
,而Reflect.getOwnPropertyDescriptor(1, 'foo')
會拋出錯誤,表示參數非法。
Reflect.isExtensible (target)
1、Reflect.isExtensible
方法對應Object.isExtensible
,返回一個布爾值,表示當前對象是否可擴展。
2、如果參數不是對象,Object.isExtensible
會返回false
,因爲非對象本來就是不可擴展的,而Reflect.isExtensible
會報錯。
Reflect.preventExtensions(target)
1、Reflect.preventExtensions
對應Object.preventExtensions
方法,用於讓一個對象變爲不可擴展。它返回一個布爾值,表示是否操作成功。
2、如果參數不是對象,Object.preventExtensions
在 ES5 環境報錯,在 ES6 環境返回傳入的參數,而Reflect.preventExtensions
會報錯。
Reflect.ownKeys (target)
Reflect.ownKeys
方法用於返回對象的所有屬性,基本等同於Object.getOwnPropertyNames
與Object.getOwnPropertySymbols
之和。
var myObject = {
foo: 1,
bar: 2,
[Symbol.for('baz')]: 3,
[Symbol.for('bing')]: 4,
};
// 舊寫法
Object.getOwnPropertyNames(myObject)
// ['foo', 'bar']
Object.getOwnPropertySymbols(myObject)
//[Symbol(baz), Symbol(bing)]
// 新寫法
Reflect.ownKeys(myObject)
// ['foo', 'bar', Symbol(baz), Symbol(bing)]