Skip to content


Subversion checkout URL

You can clone with
Download ZIP


{}.hasOwnProperty throws an error #56

shamansir opened this Issue · 11 comments

5 participants


{}.hasOwnProperty(...) in hasOwnProperty article throws an error. Seems the right way to do is:



Thanks, this is now fixed with 3a86bd3


Ok, we've also fixed it in Russian translation.


Not exactly. Object literals don't need to be parenthesized when being used as expressions. Think of it as the difference between function definitions and named function expressions. When parenthesized, the RHS of an assignment, etc. they are NFEs. Same thing going on here. Try var hop = {}.hasOwnProperty.


So we need to change the example code to

var hop = {}.hasOwnProperty;, 'foo');

this one?

Because the initial code do throws an error.


I think changing it to

console.log({}, 'bar')); // true

would be for the best. Reverting it would be awkward, because the original code is technically speaking incorrect. This should do nicely.

@ZhangYiJiang ZhangYiJiang reopened this

The original example was fine, as long as it's used as an expression, not considered an entire program. The value of that expression is still true, no console.log() necessary.


Why make it so unreadable (and slightly misleading) by involving console.log, call etc.? It's even impossible to use call in the case of calling a method on the object literal (since you have no reference to the object). There are several ways to call a function on a literal (to make it an expression, as some people mentioned):

// This was suggested above
// Wrapped in parentheses, it's an expression
// Explicitly used as an expression
var result = {}.hasOwnProperty('test');

@blixt See:

The point of the whole exercise is to provide a way to call hasOwnProperty if there's any chance at all that the method is overwritten by something else. Read the section 'hasOwnProperty as a Property' under


Oh. Sorry. In that case, I vouch for:

 var o = {test: 123};
 var result = {}, 'test');

No need for parentheses. Pretty much what you said.


I vote for any code that fits the article needs and makes no errors :)

@oozcitak oozcitak referenced this issue from a commit
@ZhangYiJiang ZhangYiJiang Fixed issue #56 3a86bd3
@oozcitak oozcitak referenced this issue from a commit in oozcitak/JavaScript-Garden
@oozcitak oozcitak Fixes #56 in Turkish translation. 69ab28a

Let me know if there are issues arising from this - pretty old so closing!

@timruffles timruffles closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.