Comments on: Terse JavaScript 101 – part 1 https://j11y.io/javascript/terse-javascript-101-part-1/ Sun, 22 Mar 2015 15:39:22 +0000 hourly 1 https://wordpress.org/?v=5.0.13 By: Victor https://j11y.io/javascript/terse-javascript-101-part-1/#comment-2248 Tue, 14 Aug 2012 16:31:11 +0000 https://j11y.io/?p=1896#comment-2248 Something that might throw people off, just because undefined == null doesn’t mean you can do something like:

if(something == null) {}

because you will get an error saying that “something” is undefined.

you still have to do:

if(typeof something == ‘undefine’) {}

]]>
By: Togue https://j11y.io/javascript/terse-javascript-101-part-1/#comment-2247 Wed, 02 Nov 2011 01:55:19 +0000 https://j11y.io/?p=1896#comment-2247 s/Less blocks/Fewer blocks/. Good example, though. I’ve reduced the line count in some source files by >50% by collapsing all the useless if(…) return true; else return false; garbage (which in turn makes other redundant lines more apparent).

]]>
By: David Higgins https://j11y.io/javascript/terse-javascript-101-part-1/#comment-2246 Fri, 28 Oct 2011 14:34:09 +0000 https://j11y.io/?p=1896#comment-2246 All valid points you have made there James, except there are times in my coding when I have to do the opposite of what you claim I should not do.

If you read plenty of books on the subject of JavaScript, all the authors never use should / should not, they simply explain the language, and leave it to the programmer to make his choice on syntax.

Remember JS is not like C where the coder has no choice but to organize his code religiously.

JS – It’s a do what the fuck you like language. That is, if you can even call it a language at all.

]]>
By: Rob https://j11y.io/javascript/terse-javascript-101-part-1/#comment-2245 Wed, 26 Oct 2011 22:52:16 +0000 https://j11y.io/?p=1896#comment-2245 Hi @Anton and @Jamie,

Describing two slightly different scenarios; If the value is unknown and you only declare the variable and don’t instantiate it in the var statement then an error will be thrown.

This throws an error (unless local4 and local5 have already been instantiated elsewhere within the global space):

 var local1 = 'local1',
        local2 = 'local2',
        local3;
        local4,
        local5;

As opposed to the following code which doesn’t fail, but has globals.

 var local1 = 'local1',
        local2 = 'local2',
        local3 = ‘local3’;
        local4 = ‘local3’,
        local5 = ‘local3’;

Here’s the fiddle http://jsfiddle.net/VXdQv/3/

Sorry I missed your point to begin with : )

]]>
By: Jamie https://j11y.io/javascript/terse-javascript-101-part-1/#comment-2244 Wed, 26 Oct 2011 14:59:40 +0000 https://j11y.io/?p=1896#comment-2244 @Anton you are correct – but anyone who codes any substantial javascript without using a syntax checking tool like jslint/jshint is nuts. This shouldn’t be an issue.

]]>
By: Anton Kovalyov https://j11y.io/javascript/terse-javascript-101-part-1/#comment-2243 Wed, 26 Oct 2011 14:15:24 +0000 https://j11y.io/?p=1896#comment-2243 @Rob,

No, you can it see for yourself here: http://jsfiddle.net/VXdQv/1/. Omitting var simply creates an implicit global variable.

]]>
By: Rob https://j11y.io/javascript/terse-javascript-101-part-1/#comment-2242 Wed, 26 Oct 2011 05:52:48 +0000 https://j11y.io/?p=1896#comment-2242 When formatting a single var statement, I like to put the comma on the next line. It gives a visual queue that it’s all one statement. Also makes it more apparent to the dev that you shouldn’t bang on a semi-colon, I read from left to right so I’m more likely to pick up the comma.

Something along the lines of:

var 
   anonname
,  funct
,  functions
,  global
,  implied
,  inblock
,  indent
,  jsonmode
,  wsh = { /* ... */ }
,  functionicity = [
	'closure', 'exception', 'global', 'label',
	'outer', 'unused', 'var'
   ];

@Anton, if you popped in a semi-colon half way along, wouldn’t the interpreter would throw an error rather than cause the consecutive declarations to be global.

]]>
By: Anton Kovalyov https://j11y.io/javascript/terse-javascript-101-part-1/#comment-2241 Mon, 24 Oct 2011 20:27:39 +0000 https://j11y.io/?p=1896#comment-2241 The combining var technique can lead to some very unreadable code and easy-to-make mistakes:

/* From JSHint/JSLint source code */
 
var anonname,
 
    /* 300 lines later we are still declaring variables */
 
    funct,          // The current function
 
    functionicity = [
        'closure', 'exception', 'global', 'label',
        'outer', 'unused', 'var'
    ],
 
    functions,      // All of the functions
 
    global,         // The global scope
    implied,        // Implied globals
    inblock,
    indent,
    jsonmode,
 
    /* ... */
 
    wsh = { /* ... */ };

I have seen numerous times when a person—who is new to the code base—comes in and accidentally ends a line with a semicolon instead of a comma making all consecutive declarations implicitly global. Oops.

]]>
By: Ext https://j11y.io/javascript/terse-javascript-101-part-1/#comment-2240 Mon, 24 Oct 2011 15:57:12 +0000 https://j11y.io/?p=1896#comment-2240 Glad to see (and hope everyone reads) the additional comments about null vs. undefined vs. ‘undefined’ vs. ”.

The differences could be considered “semantic”. But there are bug and performance considerations for each evaluation type.

@Rick Waldron: +1 to the single-vs-double quotes too. Too much time is wasted tracking down JS issues caused by closing js expressions with the wrong quote — or inside DOM object events (onclick=”doThisWith(“that”);”). Our code review policy includes single quotes for JS, double for HTML attributes, always.

Thanks for the post and the excellent comments that have resulted.

]]>
By: bionoid https://j11y.io/javascript/terse-javascript-101-part-1/#comment-2239 Sun, 23 Oct 2011 11:20:51 +0000 https://j11y.io/?p=1896#comment-2239 I did not know you could exclude the parenthesis when using “new”. However one point I would like to add is that you cannot call a method directly from your new object when you do this, eg:

var d = new Date.getTime(); /*Fails*/

And just for personal preference I also only use 1 var at the top but formatted using tabs, so it appears like a declaration block (I use jslint alot):

{
	var
		one   = 1,
		three = 3,
		nine  = 9;
 
	return one + three + nine;
}

Btw, I like your background. It gives a subtle illusion of distance 🙂

]]>