這是作為函數的 in 運算符,它返回一個布爾值,指示是否存在自己的或繼承的屬性。
用法
下麵給出的是該函數的語法has(),其中,
target是要在其中查找屬性的目標對象。
propertyKey是要檢查的屬性的名稱。
Reflect.has(target, propertyKey)
示例
下麵的例子創建了一個類的實例Student使用反射並驗證屬性是否存在使用Reflect.has()方法。
<script>
class Student{
constructor(firstName,lastName){
this.firstName = firstName
this.lastName = lastName
}
get fullName(){
return `${this.firstName}:${this.lastName}`
}
}
const args = ['Tutorials','Point']
const s1 = Reflect.construct(Student,args)
console.log(Reflect.has(s1,'fullName'))
console.log(Reflect.has(s1,'firstName'))
console.log(Reflect.has(s1,'lastname'))
</script>
上麵代碼的輸出將如下麵提到的那樣——
true true false
相關用法
- ES6 Reflect.set()用法及代碼示例
- ES6 Reflect.construct()用法及代碼示例
- ES6 Reflect.apply()用法及代碼示例
- ES6 Reflect.get()用法及代碼示例
- ES6 RegExp split()用法及代碼示例
- ES6 RegExp replace()用法及代碼示例
- ES6 RegExp exec()用法及代碼示例
- ES6 RegExp test()用法及代碼示例
- ES6 RegExp toString()用法及代碼示例
- ES6 RegExp search()用法及代碼示例
- ES6 Array every()用法及代碼示例
- ES6 Array reduceRight()用法及代碼示例
- ES6 handler.set()用法及代碼示例
注:本文由純淨天空篩選整理自 ES6 - Reflect.has()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。