Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hasOwnProperty quirk #53

Closed
dvv opened this issue Apr 15, 2011 · 9 comments
Closed

hasOwnProperty quirk #53

dvv opened this issue Apr 15, 2011 · 9 comments

Comments

@dvv
Copy link

dvv commented Apr 15, 2011

Hi!

https://github.com/Flotype/now/blob/master/lib/nowUtil.js#L86:

Sometimes causes the falsy negatives -- i.e. I pass a hash which surely is created from scratch (by literal notation) and the function returns false. When I change it to be Object.hasOwnProperty.call(currVar, prop) the result is positive.

Notice, that in CoffeeScript (which compiles to a "least common denominator" JS code, which runs on every JS engine), they use namely the said signature -- Object.hasOwnProperty.call(obj, prop) to determine whether a property is own.

I'd recommend to introduce a helper function __isOwnProp(obj, propName){return Object.hasOwnProperty.call(obj, propName)} and rewrite all invocations of obj.hasOwnProperty(prop).

Sorry for being didactic, and TIA,
--Vladimir

@sridatta
Copy link
Contributor

Thanks @dvv. We'll change it wherever we make that call. Do you know why a direct call to hasOwnProperty is not consistent?

@dvv
Copy link
Author

dvv commented Apr 15, 2011

Have no idea so far. I guess because obj.hasOwnProperty itself can be overridden.
update: they use __hasProp = Object.prototype.hasOwnProperty (notice prototype)

update2: exactly because obj.hasOwnProperty itself can be tainted/overridden

update3: this might be the reason why this change fixes the issue: there also exist objects which have no hasOwnProperty in their prototype -- the most prominent is null ;)

@ericz
Copy link
Contributor

ericz commented Apr 21, 2011

Javascript is a silly language. I suppose this is something that should be changed. Iteration through objects is a mess in this language.

@dvv
Copy link
Author

dvv commented Apr 21, 2011

Sure. Two options: rely upon underscore, which is all about convenient iterations over "lists", normalizing hashes and arrays; or CS which has clean syntax for that ;) The third option is to very carefully code inline patterns, or reinvent own set of helpers -- this usually leads to nowhere in the long run.

@erichocean
Copy link

@dvv Why not fork and write you own CoffeeScript version? Sounds like it's so easy and convenient that you'd be done in a few hours.

@dvv
Copy link
Author

dvv commented Apr 21, 2011

Of course not. I'm just trying to help biting problems I see. Plus, it will be useless unless it's accepted into the master. The whole point I'm asking whether CS source is feasible to have is to explore the field of your opinions. I'm not trying to force using CS in any way.

@ericz
Copy link
Contributor

ericz commented Apr 21, 2011

Thanks for both of your contributions guys. Dvv has been very polite in helping us out. More awareness of best practices can't hurt.

@ericz
Copy link
Contributor

ericz commented May 9, 2011

@dvv I have changed all obj.hasOwnProperty to Object.hasOwnProperty.call

Is there a benefit of using Object.prototype.hasOwnProperty.call vs Object.hasOwnProperty.call ??

@ericz ericz closed this as completed May 9, 2011
@dvv
Copy link
Author

dvv commented May 10, 2011

Thanks!

I guess the latter costs one more hop along the prototype chain. They just teach us augmenting/mangling Object.prototype is bad practice, so they assume Object.prototype.hasOwnProperty is pristine in either case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants