4 ms·
Woah, missed a whole section: > This is not a valid construct. By using "of" you are iterating through the keys of dishes, which will inevitable be strings. N
by ryanflorence 15y ago
Woah, missed a whole section:
> This is not a valid construct. By using "of" you are iterating through the keys of dishes, which will inevitable be strings.
No, they are objects with properties like "dirty", not strings. Dishes is an object of key value pairs. I've been very active in MooTools, contributed to its source, and have written plenty of my own stuff to know the importance of iterating arrays v. objects properly, (and have since repented of extending built-ins).
While I'm on the topic, I love `for own key, val of obj`, own is so, so awesome right there.
- YmMot 15y agoOk, just to be clear because this comment is ambiguous as to if you understand what I'm saying. The comprehension in your article is not valid, you need to change one thing or the other for it to work. > Dishes is an object of key value pairs Yes, and you are getting the KEYS, not the VALUES. I doubt I need to say this, but per the spec KEYS are ipso facto strings. At the point you do plate.dirty "plate" is a STRING, one of the key names in the dishes object. See, here is the compiled code fresh from the compiler (with added comments): var plate; if (meal.status === 'done') { for (plate in dishes) { // plate is a STRING, plate.dirty will not exist // you want dishes[plate].dirty if (plate.dirty) wash(plate, brush, sink); } } You're making the mistake of thinking CS will do the plate = dishes[ key ] part for you, but it won't...do it yourself or us "in" on an array. You'll also note there are no sanity checks, so use with caution. This is precisely what I mean when I talk about not knowing CS well enough. I mean this with all due respect and do not intend to dismiss you. You are clearly very bright and a good coder. Anyone will get tripped up in any language at first. The point is I've used CS so much I breathe this stuff...I knew instantly that it was an invalid construct.... I'm simply saying (like any language) once you learn it well enough a lot of these sorts of issues fade into the background. Maybe you don't think it's worth the effort to get to this point, and that would be fair enough. I just think it needs saying that some of the points you raise are related to being relatively new to CS.
- ryanflorence 15y agoDishes is an object, a dish is an object. I know what I'm doing :) dishes = { ryansPlate: { dirty: true }, yourPlate: { dirty: false }, hisPlate: { dirty: false } } Now go into the debugger, find the _ref, and then add a break point, inspect it, and you'll see. Or if it was just JS, you wouldn't have to mess with _ref, you'd just put in a break point right where you were last looking at the code wondering "what kind of object is dishes?" Edit: Oh snapz you got me! for key, plate of dishes Missed the `key`, post updated. Go ahead an chalk that up to "Ryan doesn't know CS", if you believe it's any more incriminating than missing any ol' arg in a function signature.
- YmMot 15y agoSure, and here's the issue: At the point you do plate.dirty plate === "ryansPlate" In CoffeeScript the construct for foo of bar Iterates through the KEYS of bar in turn, assigning them to foo...NOT the values To be more specific, it iterates through PROPERTY NAMES. Per the JS spec, property names are always strings (you can't use objects...but you know this I'm just being thorough.) In your example, you do plate.dirty At this point, plate is equal to "ryansPlate" on the first iteration, "yourPlate" on the second, etc. (well technically the order is undefined, but you get my point) You want dishes[plate].dirty To be absolutely clear, I'm don't want to make this out to be anything other than the sort of stupid error all of us make all the time. I'm simply pointing out that I recognize it much quicker because I have a very intuitive understanding of CS at this point. Where you, who are very good at (and used to) JS have a harder time seeing the error. Likewise, I'm sure that you're aware of the dangers of for-in, I'm simply pointing out that it does no sanity checks so watch out. Also to be clear, I'm talking about your example as written in the article. Maybe you have some other version that works. Go back to my other comment, I compiled your CS code with the CS compiler. You'll see the error there. If you like I'll make a JSFiddle that demonstrates the problem. I think you've getting stuck on the fact that in my original criticism I didn't know if dishes was an Array or an Object because it wasn't clear from your code...to be clear it is wrong both ways...so I simply said "it's wrong, something must be changed" apologies if this was ambiguous. Now that you've clarified, it's still wrong...just a particular kind.