ES6 - Reflect.has()

这是一个 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