Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

{}.hasOwnProperty throws an error #56

Closed
shamansir opened this Issue · 11 comments

5 participants

@shamansir
Collaborator

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

({}).hasOwnProperty(...).

@ZhangYiJiang
Collaborator

Thanks, this is now fixed with 3a86bd3

@shamansir
Collaborator

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

@michaelficarra

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.

@shamansir
Collaborator

So we need to change the example code to

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

this one?

Because the initial code do throws an error.

@ZhangYiJiang
Collaborator

I think changing it to

console.log({}.hasOwnProperty.call(foo, '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
@michaelficarra

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.

@blixt

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
({}).hasOwnProperty('test')
// Wrapped in parentheses, it's an expression
({}.hasOwnProperty('test'))
// Explicitly used as an expression
var result = {}.hasOwnProperty('test');
@ZhangYiJiang
Collaborator

@blixt See: https://github.com/BonsaiDen/JavaScript-Garden/issues/closed#issue/4

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 http://bonsaiden.github.com/JavaScript-Garden/#object.hasownproperty

@blixt

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

 var o = {test: 123};
 var result = {}.hasOwnProperty.call(o, 'test');

No need for parentheses. Pretty much what you said.

@shamansir
Collaborator

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
@timruffles
Collaborator

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.