douglascrockford / JSON-js

JSON in JavaScript
http://www.JSON.org/
8.69k stars 4.59k forks source link

(fix) hasOwnProperty is an Object instance method #91

Closed philBrown closed 7 years ago

philBrown commented 7 years ago

Change incorrect references of

Object.hasOwnProperty

to

Object.prototype.hasOwnProperty
douglascrockford commented 7 years ago

What is the bug that this fixes?

philBrown commented 7 years ago

According to the specification (and the browser I was trying to use this with), hasOwnProperty is an Object.prototype method and Object.hasOwnProperty does not exist

douglascrockford commented 7 years ago

That does not answer my question. What bug does this repair?

philBrown commented 7 years ago

Running the current parse_json function (from either json_parse.js or json_parse_state.js) in a browser that implements only the specification (ie, does not implement Object.hasOwnProperty as a function) results in a runtime error to the tune of

Cannot read property 'call' of undefined

This PR fixes that error by using Object.prototype.hasOwnProperty which is part of the specification and is implemented by a wider range of browsers.

Would you like me to raise an issue first?

douglascrockford commented 7 years ago

What browser does this?

philBrown commented 7 years ago

The one I was having trouble with was an embedded ANT browser.

I know you probably don't aim to support something like this but making this change works correctly. As far as I can tell, there is no official documentation for hasOwnProperty as a static method of Object and apart from the two line changes in this pull request, all other calls to the method are made via Object.prototype.hasOwnProperty so if nothing else, this makes the code consistent.

douglascrockford commented 7 years ago

This code is stable and near end of life. I will not make changes unless they fix real problems.

Object inherits from Object.prototype, so correct implementations will find hasOwnProperty.