Documentation Suggestion: Remove quotes from keys in javascript object

Documentation Suggestion: Remove quotes from keys in javascript object

tacman1123tacman1123 Posts: 198Questions: 46Answers: 1
edited June 2011 in General
Loving the new Datatables 1.8, playing with the Scroller now, will probably adopt site-wide soon.

Would you consider changing your documentation style to not quote the object keys? I think it improves readability -- it's a tiny thing, but wanted to propose it anyway. Here's the difference:

[code]
$(document).ready(function() {
$('#example').dataTable( {
"sScrollY": "200px",
"sAjaxSource": "media/dataset/large.txt",
"sDom": "frtiS",
"bDeferRender": true
} );
} );
[/code]

Without quoting the keys:

[code]
$(document).ready(function() {
$('#example').dataTable( {
sScrollY: "200px",
sAjaxSource: "media/dataset/large.txt",
sDom: "frtiS",
bDeferRender: true
} );
} );
[/code]

Just a thought.

Tac

Replies

  • allanallan Posts: 63,205Questions: 1Answers: 10,415 Site admin
    The reason I do it that way is because it is closer to being valid JSON (which does require keys to be quoted in that manner). Also I think it removes any possible confusion about variables having the same name as the key - for example:

    [code]
    var a = 123;
    var b = {
    a: 456
    };
    [/code]
    In the above is the 'a' in the 'b' object a string literal or 123? It is of course just 'a', but personally I find that a bit odd and possibly confusing.

    Generally I try to align Javascript objects:

    [code]
    $(document).ready(function() {
    $('#example').dataTable( {
    "sScrollY": "200px",
    "sAjaxSource": "media/dataset/large.txt",
    "sDom": "frtiS",
    "bDeferRender": true
    } );
    } );

    // or
    $(document).ready(function() {
    $('#example').dataTable( {
    "sScrollY": "200px",
    "sAjaxSource": "media/dataset/large.txt",
    "sDom": "frtiS",
    "bDeferRender": true
    } );
    } );
    [/code]
    but I've obviously been a bit lax in places :-(. Readable documentation is what is wanted though (given that I use it a lot myself, I want that too!) so I'll look at how this can be improved.

    Thanks for the feedback!
    Allan
  • tacman1123tacman1123 Posts: 198Questions: 46Answers: 1
    Thanks. I realize my forum post title is in error -- it is valid JSON, but what I was proposing was that it be a Javascript object, and not JSON (JSON is valid JS, not vice-versa). So what I should have called this post was

    Documentation Suggestion: Remove quotes from keys in Javascript object

    While I understand your logic, I still prefer to see js objects without quotes, it looks better in the formatted js in this forum, and it feels more "right" to me -- "abc": "def" to me looks like a pair of strings, where abc: "def", has the feel of assignment.

    Anyway, my two cents, thanks for listening!

    Tac
  • allanallan Posts: 63,205Questions: 1Answers: 10,415 Site admin
    I'll have a look at the syntax highlighter I use on the site and see if can be tweaked to colour the keys differently from regular strings - that would probably help to some degree.

    Regards,
    Allan
This discussion has been closed.