Skip to content

Instantly share code, notes, and snippets.

@Meettya
Created May 10, 2012 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Meettya/2652639 to your computer and use it in GitHub Desktop.
Save Meettya/2652639 to your computer and use it in GitHub Desktop.
why sugar is so sweet
# underscore.js
target[key] = if _.isArray val then [] else {}
# sugar.js
target[key] = if val?.isArray then [] else {}
@andrewplummer
Copy link

I think you need () on isArray here.

Also as much as I want to agree with the title of this gist, in this case, for most people at least, it should actually match underscore as you need to put either Object.isArray(val) or Array.isArray(val) (the latter is ES5 standard).

The above code is only valid if you explicitly map Object methods to the Object.prototype like this:

Object.extend();

This is easy enough but it's off by default as Sugar doesn't interfere with Object.prototype so as not to break stuff....

@Meettya
Copy link
Author

Meettya commented May 10, 2012

Oh, I get this line from full-sized worked CoffeeScript code, and its looks like broken, yap, sorry.

I re-check JS result, it compiled correctly without () (CS very smart in braces) but yes, val need to be extended before test, I missed it.

Thanx for you comment.

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