Felix King accurately considered the issue of inheritance, so I will consider the concept of existing properties
If you are just trying to check for the existence of a property called prototype on an object, you can use:
a.hasOwnProperty('prototype')
This will return true for:
a = { //the object has this property, even though //it will return undefined as a value prototype: undefined };
This assumes that the object is not processed as a hash map where other properties, such as hasOwnProperty , are hasOwnProperty , otherwise a safer way to check for the presence of a property is:
Object.prototype.hasOwnProperty.call(a, 'prototype')
This can be turned into a general function like:
has = (function (h) { "use strict"; return function (obj, prop) { h.call(obj, prop); }; }(Object.prototype.hasOwnProperty));
and is used as:
has(a, 'prototype');
source share