Skip to content Skip to sidebar Skip to footer

Hasownproperty('gettime') Returns False On Date Object

const test = new Date() test.hasOwnProperty('getTime') // false 'getTime' in test // true this means that getTime is not in the prototype of test (not it's own prototype), but fur

Solution 1:

hasOwnProperty doesn't look up the prototype chain:

Every object descended from Object inherits the hasOwnProperty method. This method can be used to determine whether an object has the specified property as a direct property of that object; unlike the in operator, this method does not check down the object's prototype chain. (source)

This is why hasOwnProperty is often used to check if a property exists, in for...in loops:

for (key in obj) {
  if (obj.hasOwnProperty(key))
    // do stuff with obj[key]
  }
}

Solution 2:

getTime its in prototype so it gives false when you using in operator it delegates through the prototype chain and if finds any property return true otherwise false

Post a Comment for "Hasownproperty('gettime') Returns False On Date Object"