JavaScript: Reflect.has
New in JS2015.
Reflect.has(obj, key)
-
Return
true
if obj has property key key (string key or symbol key), or in prototype chain. Elsefalse
. Similar tokey in obj
[see “in” Operator] , but as a function instead of a operator.
const u = {p:3}; console.log( Reflect.has ( u , "p" ) ); // true console.log( Reflect.has ( u , "y" ) ); // false
Example with property key in parent object.
// Reflect.has return true if property is own property or in prototype chain const ob1 = {p:3}; const ob2 = Object.create (ob1); console.log( Reflect.has ( ob2 , "p" ) ); // true
Example with symbol key.
// Reflect.has works with both string and symbol keys const sy = Symbol(); const ob = {}; ob["st"] = 3; // add a string key ob[sy] = 4; // add a symbol key console.log( Reflect.has ( ob , "st" ) ); // true console.log( Reflect.has ( ob , sy ) ); // true
[see Symbol Tutorial]
[see Access Property]