Sunday, May 12, 2013

Destructuring Assignment: Another Case for CoffeeScript

Often times, a single options parameter is passed into a JavaScript function.

In that called function, we will then split up the options argument into separate variables to used later.

Javascript

Here's how it's frequently done in Javascript:


var options;

options = {
  id: 1,
  name: "bob",
  address: "123 main street",
  city: "new york",
  state: "ny",
  zip: 10027
};

({
  myFunction: function(options) {
    var address, city, id, name, state, title, zip;

    id = options.id
    name = options.name
    title = options.title
    address = options.address
    city = options.city
    state = options.state
    zip = options.zip;

    return console.log('name', name);
  }
});


CoffeeScript

Here's how it can be done in CoffeeScript:


options =
  id: 1
  name: "bob"
  address: "123 main street"
  city: "new york"
  state: "ny"
  zip: 10027
  

myFunction: (options) ->
  { id, name, title, address, city, state, zip } = options  # << Destructing Assignment
  console.log 'name', name


The CoffeeScript implementation, using Destructing Assignment is a nice improvement.



Javascript 1.7

Granted Javascript 1.7 provides Destructing Assignment, but if you rely on that, then you'll need to worry about end user browser compatibility and the higher likely hood of developers falling into the trap of using proprietary extra non-cross-browser features, e.g., Mozilla's for each.

Conclusion

This is one small example of why you should be using CoffeeScript instead of raw Javascript.

Here are a few more reasons to use CoffeeScript:
  • Fewer lines to write and maintain
  • More expressive (easier to understand) code
  • String interpolation
  • Consistent, readable, pretty-printed generated Javascript
  • Generally runs faster than typical Javascript hard-coded "equivalent" code


References

http://jashkenas.github.io/coffee-script/
http://js2coffee.org/


22 comments:

  1. great article. its helpful for me. we also provide web services in lahore visit us Web development services in Lahore

    ReplyDelete
  2. Its a great pleasure reading your post.Its full of information I am looking for and I love to post a comment that "The content of your post is awesome" Great work.

    seo services

    ReplyDelete
  3. Thank you for the update, very nice site..

    seo company

    ReplyDelete

  4. Thank you for taking the time to publish this information very useful!

    digital marketing agency

    ReplyDelete
  5. This blog is so nice to me. I will keep on coming here again and again. Visit my link as well..

    cheap web hosting in lahore

    ReplyDelete
  6. Thanks for sharing this amazing post. I am a Writer at Assignments Planet that provides online assignment writing service for students in the UK. It has professional writers that will make you the best assignment on any topic you provide.

    ReplyDelete
  7. It is truly a well-researched content and excellent wording. I got so engaged in this material that I couldn’t wait reading. I am impressed with your work and skill. Thanks.
    Best SEO Services in Lahore

    ReplyDelete
  8. Wow, excellent post. I'd like to draft like this too - taking time and real hard work to make a great article. This post has encouraged me to write some posts that I am going to write soon.
    Best SEO Services

    ReplyDelete
  9. Web Development Company in Lahore is the leading provider of web development products and services in Pakistan. From complete web design to custom application development, our team can create any digital solution you can imagine.

    ReplyDelete
  10. We offer a wide range of services like web development, website design, ecommerce stores and blogs. We develop sites for both PC and mobile devices.

    ReplyDelete
  11. The company is to become the best website designing company in Lahore, by providing reliable Affordable Website Designing Services, with a work culture that is positive, creative and dynamic.

    Web Designing Company
    Social Media Marketing

    ReplyDelete
  12. Most web site design company executives can talk fluent English and feature pretty appropriate communication skills.

    ReplyDelete
  13. We offer a range of services that include SEO, web designing, mobile application development and internet marketing.

    ReplyDelete
  14. A web development company is a business that specializes in developing websites.

    ReplyDelete
  15. A nice website development is a must-have for every business that wants to succeed.

    ReplyDelete
  16. Web development Lahore and innovative team work closely to upgrade the era used on your internet site.

    ReplyDelete
  17. Ecommerce is a great way to promote customised products. Visit if you're looking for the best eCommerce App development company in the US.

    ReplyDelete
  18. Really enjoyed your post! I loved the way you explored the topic from different angles and offered practical advice. Do you have any recommendations for further reading or resources on this? Can’t wait to dive deeper!

    ReplyDelete