The Art of Readable Code by Boswell Dustin Foucher Trevor & Trevor Foucher

The Art of Readable Code by Boswell Dustin Foucher Trevor & Trevor Foucher

Author:Boswell, Dustin, Foucher, Trevor & Trevor Foucher [Dustin Boswell]
Language: eng
Format: epub
Tags: COMPUTERS / General
ISBN: 9781449314217
Publisher: O'Reilly Media
Published: 2011-11-02T16:00:00+00:00


Other General-Purpose Code

When debugging JavaScript, programmers often use alert() to pop up a message box that displays some information to the programmer, the Web’s version of “printf() debugging.” For example, the following function call submits data to the server using Ajax and then displays the dictionary returned from the server:

ajax_post({ url: 'http://example.com/submit', data: data, on_success: function (response_data) { var str = "{\n"; for (var key in response_data) { str += " " + key + " = " + response_data[key] + "\n"; } alert(str + "}"); // Continue handling 'response_data' ... } });

The high-level goal of this code is, Make an Ajax call to the server, and handle the response. But a lot of the code is solving the unrelated subproblem, Pretty-print a dictionary. It’s easy to extract that code into a function like format_pretty(obj):

var format_pretty = function (obj) { var str = "{\n"; for (var key in obj) { str += " " + key + " = " + obj[key] + "\n"; } return str + "}"; };



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.