2

Both of these obviously do similar things but my question is why is one on the prototype and one on the Object?

For example, both of these called differently. Is there a logical reason why this is the case?

var o = {name: "value"}

o.hasOwnProperty("name") //true

Object.getOwnPropertyNames(o); //name 
//Couldn't the above have been coded so we can run o.getOwnPropertNames();

Thanks.

KingKongFrog
  • 13,946
  • 21
  • 75
  • 124

1 Answers1

0

The problem with adding these methods to the prototype is taht they are prone to getting overloaded.

var o = {};
o.getOwnPropertyNames = 17;
//What now?

In fact, you would often see people doing

Object.prototype.hasOwnProperty.call(o, propname);

in order to avoid the overloading issue with hasOwnProperty.

Community
  • 1
  • 1
hugomg
  • 68,213
  • 24
  • 160
  • 246