jashkenas / coffeescript

Unfancy JavaScript
https://coffeescript.org/
MIT License
16.5k stars 1.99k forks source link

own? #1019

Closed rampall closed 13 years ago

rampall commented 13 years ago

is own a keyword?

i don't see it mentioned (COFFEE_KEYWORDS) except in the documentation example: for own key, value of object

hen-x commented 13 years ago

It's a contextual keyword, so it only has special meaning when it follows for and precedes another identifier. You can still use it as an identifier; you can even legally write for own own of own if you want to be confusing.

rampall commented 13 years ago

Ah thanks!

P.S for own own of own has quite a zen feel to it - i must admit

dvv commented 13 years ago

Please, consider the following: if own prop of obj then ... which could compile to if (Object.prototype.hasOwnProperty.call(obj, prop)) {...} and would literate-ize quite a common pattern, imho. TIA, --Vladimir

michaelficarra commented 13 years ago

@dvv: own = (prop, obj) -> Object::hasOwnProperty.call obj, prop allows you to do if own prop, obj then .... Not a perfect solution, but it's probably good enough. There was a previous suggestion for this, let me look it up. Can't find it, oh well.

dvv commented 13 years ago

sure. That is what i do so far in hope own will become truly helpful keyword. The reasons: each and every file must contain this purely routine shim; one may accidently overwrite 'own' implementation; chances are that compiler already included hasOwn helper which is then purely a bloat; braces required in case of complex objects.

jashkenas commented 13 years ago

I'm afraid this shouldn't be a keyword -- it's already a method on every object. We don't need to turn Object.prototype methods into keywords just for convenience.

dvv commented 13 years ago

What if rename implicit __hasProp to own and always bundle it, so that the coffee code look clean?