small medium large xlarge

Back to: All Forums  CoffeeScript
Generic-user-small
11 Feb 2012, 21:49
Achim (6 posts)

Hello,

what construction must i use that coffeescript compiles to:

if (property in object). When i use the syntax:

if object?.property

it compiles to:

if (object != null ? object.property : void 0)

Anyone can help me?

Sorry my english isn’ the best.

Thank you.

Trevor_burnham__goofy_headshot__pragsmall
14 Feb 2012, 15:16
Trevor Burnham (54 posts)

What you want is @of@. CoffeeScript’s @of@ operator compiles to JavaScript’s @in@ operator; see section 3.3 for more on @of@ vs. @in@.

You can also write @object.property?@, which means “Is object.property non-null?” @object?.property@ means “Give me @object.property@ if @object@ is non-null.”

Generic-user-small
15 Feb 2012, 21:23
Achim (6 posts)

Thank you very much.

It was my mistake: I thought as i try if object of property that not compiles to “in” in Javascript. But now it does exactly that what you say. I have your book but till yet i had no time to begin read it, but i look forward to read it.

….But: I have now tried some things. Now i’m confussed that when i ran the construct

if letos of likes

with “coffee file” it comes an error. But it compiles perfectly to Javascript’s “if(letos in likes)” I run coffe-script with node.js on windows xp.

Other constructs runs perfectly but not “if object of property”. Is this normally?

Thank you.

Trevor_burnham__goofy_headshot__pragsmall
16 Feb 2012, 15:47
Trevor Burnham (54 posts)

Same problem as in the other thread: @letos@ is a variable, not a literal; you want @if ‘letos’ of likes@, or

str = 'letos'
if str of likes
  ...
You must be logged in to comment