Private brand checks a.k.a. #foo in obj · V8

submited by
Style Pass
2021-05-23 23:00:08

The in operator can be used for testing whether the given object (or any object in its prototype chain) has the given property:const o1 = {'foo': 0};
console.log('foo' in o1); // true
const o2 = {};
console.log('foo' in o2); // false
const o3 = Object.create(o1);
console.log('foo' in o3); // true

The private brand checks feature extends the in operator to support private class fields:class A {
static test(obj) {
console.log(#foo in obj);
}
#foo = 0;
}

A.test(new A()); // true
A.test({}); // false

class B {
#foo = 0;
}

A.test(new B()); // false; it's not the same #foo

Leave a Comment