Reflect.has()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2016.

Phương thức tĩnh Reflect.has() giống như toán tử in, nhưng dưới dạng hàm.

Try it

const object = {
  property1: 42,
};

console.log(Reflect.has(object, "property1"));
// Expected output: true

console.log(Reflect.has(object, "property2"));
// Expected output: false

console.log(Reflect.has(object, "toString"));
// Expected output: true

Cú pháp

js
Reflect.has(target, propertyKey)

Tham số

target

Đối tượng target trong đó tìm kiếm thuộc tính.

propertyKey

Tên của thuộc tính cần kiểm tra.

Giá trị trả về

Một Boolean cho biết target có thuộc tính đó hay không.

Ngoại lệ

TypeError

Được ném ra nếu target không phải là đối tượng.

Mô tả

Reflect.has() cung cấp ngữ nghĩa phản chiếu của việc kiểm tra xem một thuộc tính có trong một đối tượng hay không. Tức là, Reflect.has(target, propertyKey) tương đương về mặt ngữ nghĩa với:

js
propertyKey in target;

Reflect.has() gọi phương thức nội tại [[HasProperty]] của đối tượng trên target.

Ví dụ

Sử dụng Reflect.has()

js
Reflect.has({ x: 0 }, "x"); // true
Reflect.has({ x: 0 }, "y"); // false

// returns true for properties in the prototype chain
Reflect.has({ x: 0 }, "toString");

// Proxy with .has() handler method
obj = new Proxy(
  {},
  {
    has(t, k) {
      return k.startsWith("door");
    },
  },
);
Reflect.has(obj, "doorbell"); // true
Reflect.has(obj, "dormitory"); // false

Reflect.has trả về true cho bất kỳ thuộc tính kế thừa nào, giống như toán tử in:

js
const a = { foo: 123 };
const b = { __proto__: a };
const c = { __proto__: b };
// The prototype chain is: c -> b -> a
Reflect.has(c, "foo"); // true

Đặc tả kỹ thuật

Specification
ECMAScript® 2027 Language Specification
# sec-reflect.has

Tương thích trình duyệt

Xem thêm