introduce the has keyword as an alias for obj.hasOwnProperty(name) #2103

Closed
ollym opened this Issue Feb 4, 2012 · 14 comments

Comments

Projects
None yet

ollym commented Feb 4, 2012

We have the of operator but it would be nice to have the has operator too.

So like...

if foo has 'bar' then foo.bar() => var __hasProp = Object.prototype.hasOwnProperty; if(__hasProp.call(foo, 'bar')) foo.bar()

Collaborator

michaelficarra commented Feb 4, 2012

We also have the contextual keyword own that we may want to use instead.

Collaborator

satyr commented Feb 4, 2012

Or on:

foo.bar() if 'bar' on foo

which is already reserved.

ollym commented Feb 4, 2012

I still think has makes more sense here

I'll +1 @michaelficarra and suggest desugaring own key of obj into obj.hasOwnProperty(key) (or __hasProp(obj, key)) to mimic the for own key of obj construct.

Collaborator

michaelficarra commented Feb 5, 2012

@afriggeri:

$ coffee -bep 'own key of obj'
own(key in obj);

I'm actually not too fond of that change. I was just adding that note to the conversation as a reminder of a related syntactical construct.

Contributor

clutchski commented Feb 5, 2012

I'm not a fan of own because it doesn't read like a true/false question. has is much better in this respect.

ollym commented Feb 5, 2012

if obj has 'foo'
if obj owns 'foo'
if 'foo' on obj
if 'foo' of own obj
?

Contributor

davidchambers commented Feb 5, 2012

The problem I have with the object has property form is that it possibly reads too well in certain cases:

ol = document.createElement 'ol'
ol has 'childNodes' # true

This is somewhat confusing since ol has no child nodes is a valid statement. The property on object form is less confusing in such cases because it reads less like a sentence:

ol = document.createElement 'ol'
'childNodes' on ol # true

showell commented Feb 16, 2012

-1

I know that hasOwnProperty is fundamental in some respect, but I still don't think it justifies its own syntax. It's a pretty expensive use of a keyword to wrap just one function. I know "Object.prototype.hasOwnProperty" is kind of awkward, but it's easy enough to wrap as a helper function without complicating the coffeescript compiler implementation. It would also be one more thing to document, and there's already some confusion related to other prepositions ("in", "of").

Is there any precedent in other languages for using keywords for this kind of introspection? (I'm not saying other languages should drive the decision; they just might be instructive.)

Collaborator

TrevorBurnham commented Feb 21, 2012

I like @satyr's suggestion of on. The big problem with has as a keyword is that it only reads grammatically if you write

obj has key

which contrasts with

key of obj

whereas key on obj is quite similar, as it should be: It's just a more restrictive key of obj.

This would also let us write for key on obj loops, rather than the ever-awkward for own key of obj.

Contributor

davidchambers commented Feb 22, 2012

This would also let us write for key on obj loops, rather than the ever-awkward for own key of obj.

Great point, Trevor. I had not considered for own key of obj until you mentioned it. It makes sense to me that if we adopt a shorthand, it should be something that reads well in both contexts. On these grounds, on is the best of the suggestions to date.

satyr referenced this issue in satyr/coco May 21, 2013

Open

own in #224

+1 for adding syntax for hasOwnProperty (I tend to use it fairly often, and it always feels like boilerplate that CS should automate). I think own is the right keyword to use, so that the same thing uses the same syntax everywhere.

So own key of obj is a winner for me.

-1 for obj has key; while it reads well enough, it reverses the order of operands, PHP-style.

blitmap commented Sep 30, 2014

@nami-doc: This is not what I am proposing in that other issue. I'm talking about values, not properties/keys.

Owner

jashkenas commented Sep 30, 2014

In general, we don't need to add keywords where convenient methods already exist.

if obj.hasOwnProperty 'foo'

... is a fine way to write it.

jashkenas closed this Sep 30, 2014

jashkenas added the wontfix label Sep 30, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment