Answers for "hasownproperty ts"

2

hasOwnProperty

The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).

const object1 = {};
object1.property1 = 42;

console.log(object1.hasOwnProperty('property1'));
// expected output: true

console.log(object1.hasOwnProperty('toString'));
// expected output: false
Posted by: Guest on December-21-2020
0

hasOwnProperty

//
//2021 - Object.hasOwn as a replacement for Object.hasOwnProperty()

//As other answers indicated, hasOwnProperty will check for an object own properties in contrast to in which will also check for inherited properties.

//There is a new alternative method called Object.hasOwn() and is intended to be a replacement for Object.hasOwnProperty()**

const person = { name: 'dan' };

console.log(Object.hasOwn(person, 'name'));// true
console.log(Object.hasOwn(person, 'age'));// false

const person2 = Object.create({gender: 'male'});

console.log(Object.hasOwn(person2, 'gender'));// false
 Run code snippet
Posted by: Guest on November-15-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language