Comments on: Terse JavaScript 101 – part 2 https://j11y.io/javascript/terse-javascript-101-part-2/ Sun, 22 Mar 2015 15:39:22 +0000 hourly 1 https://wordpress.org/?v=5.0.13 By: Jason https://j11y.io/javascript/terse-javascript-101-part-2/#comment-2292 Wed, 04 Apr 2012 22:45:37 +0000 https://j11y.io/?p=1937#comment-2292 James,

One of the misleading things of

for(var i = 0; i < arr.length; i++) { }

is "i" is hoisted to the top of the object scope and not contained within the for loop. I would suggest declaring it outside the loop at the top as a general practice and then setting it like so:

// declared at top of object scope (function scope)
var i;
// … code
for(i = 0; i < arr.length; i++) { }

This is anal but its important for people not to misunderstand how JavaScript works as you are getting a lot of traffic so you have more responsibility to be a good role model. Lots of good stuff here by the way.

]]>
By: Joe Zim https://j11y.io/javascript/terse-javascript-101-part-2/#comment-2291 Wed, 15 Feb 2012 06:40:23 +0000 https://j11y.io/?p=1937#comment-2291 @Strx
Interesting trick. I never knew the tilde even did anything before. For anyone who’s wondering themselves, check out http://dreaminginjavascript.wordpress.com/2008/07/04/28/ for more info. I kinda wonder who came up with the idea for using the ~ like that because whoever (s)he is, (s)he probably should see a psychiatrist. 😉

Maybe I should do a short post about it on my blog, but I’m not 100% sure. We’ll see.

]]>
By: Caio Ribeiro Pereira https://j11y.io/javascript/terse-javascript-101-part-2/#comment-2290 Wed, 25 Jan 2012 22:24:59 +0000 https://j11y.io/?p=1937#comment-2290 Nice post man!!

It will be very useful this tuts for me!!

Just to complement, look some of my posts about Javascript Design Patterns

http://www.udgwebdev.com/design-patterns-para-javascript-parte-1/

http://www.udgwebdev.com/design-patterns-para-javascript-parte-2/

I know its in portuguese, but I’m very proud to be a brazilian! 😀

]]>
By: alpha123 https://j11y.io/javascript/terse-javascript-101-part-2/#comment-2289 Tue, 08 Nov 2011 21:23:42 +0000 https://j11y.io/?p=1937#comment-2289 @Rob: in is really slow for looping Arrays (or anything else). See http://jsperf.com/for-loop-vs-for-in-loop. hasOwnProperty, which is basically required if you’re doing things that way, makes it even slower.

]]>
By: Rob Williams https://j11y.io/javascript/terse-javascript-101-part-2/#comment-2288 Tue, 08 Nov 2011 08:28:41 +0000 https://j11y.io/?p=1937#comment-2288 @alpha123 – Do you mean using ‘in’ is really slow for looping arrays or adding a hasOwnProperty check is slow? I’m aware of the behaviour you demonstrated, hence the example including looping the properties of an object, and agree this is a bad idea to use as a matter of course. However the post is surrounding terseness and when for example looping an array of JSON data with a fixed predefined structure is there any real harm?

]]>
By: alpha123 https://j11y.io/javascript/terse-javascript-101-part-2/#comment-2287 Tue, 08 Nov 2011 06:14:10 +0000 https://j11y.io/?p=1937#comment-2287 @Garciat:

Yes, I know about forEach. The reason I wrote it that way was as an example to show the implementation (which definitely isn’t ES5 compliant), for his “expressive diversity” thing.

A regular for loop is always going to be faster than a forEach call. Method calls are expensive, the browser has to allocate a new function object, and the callback function is called for every element of the array. Compared to a low-level loop, it’s quite slow.

@Rob:

BAD IDEA.

Object.prototype.method = function () { return 42; };
Array.prototype.func = function () { return 'beef'; };

var o = {
  p1: 1,
  p2: 2,
  p3: 3
};
 
var a = [1, 2, 3];
 
for(var i in o) {
  alert(o[i]);
}
// Alerts 1, 2, 3, function () { return 42; }
 
for(var i in a) {
  alert(a[i]);
}
// Alerts 1, 2, 3, function () { return 'beef'; }, function () { return 42; }

This can be fixed by adding a hasOwnProperty check to the loops:

(function (hasOwn) {

var i;

for (i in o) {
  if (hasOwn.call(o, i))
    alert(o[i]);
}
// Alerts 1, 2, 3

for (i in a) {
  if (hasOwn.call(a, i))
    alert(a[i]);
}
// Alerts 1, 2, 3

})({}.hasOwnProperty);

But you shouldn’t do that with arrays because it’s really slow. Even a forEach call is faster.

@Strx: That’s cute. I usually use + for coercion to a number, but that returns NaN for ‘a’, undefined, function () { }, etc (all of which ~~ returns 0 for). Still, + is going to be faster, and ~~ floors the operand (incorrectly no less, try ~~-1.2).

@Tom: -‘222’ returns -222. You probably want -(-‘222′) which behaves exactly the same as +’222’ (where ‘222’ can be anything).

]]>
By: Tom https://j11y.io/javascript/terse-javascript-101-part-2/#comment-2286 Mon, 07 Nov 2011 13:50:38 +0000 https://j11y.io/?p=1937#comment-2286 Wouldn’t it be better to convert a string to number with –?
var str = ‘222’;
var num = –str; // => 222

typeof num; // => ‘number’

+ is used for string concatenation as well, so I don’t really know if it’s a good idea to use it?

]]>
By: Darragh https://j11y.io/javascript/terse-javascript-101-part-2/#comment-2285 Sat, 05 Nov 2011 10:51:31 +0000 https://j11y.io/?p=1937#comment-2285 Excellent article thanks – to be pedantic a function which is a member of an object is referred to as a method.

Thanks again. Darragh

]]>
By: Joshua https://j11y.io/javascript/terse-javascript-101-part-2/#comment-2284 Fri, 04 Nov 2011 21:14:05 +0000 https://j11y.io/?p=1937#comment-2284 Of course using call or apply could help you both shorten the code and not lose the context.

For your last example you could do

var obj = {
    value: 234,
    fn: function(){ return this.value; }
};
 
obj.fn(); // = 234
 
var fn = obj.fn;
fn.call(obj); // = 234
]]>
By: Rob Williams https://j11y.io/javascript/terse-javascript-101-part-2/#comment-2283 Fri, 04 Nov 2011 17:06:57 +0000 https://j11y.io/?p=1937#comment-2283 You don’t mention looping with the ‘in’ operator. I’m no expert so can’t say whether it is a good or a bad thing in terms of performance but it is nice and terse…

  var o = {
    p1: 1,
    p2: 2,
    p3: 3
  };
 
  var a = [1, 2, 3];
 
  for(var i in o) {
    alert(o[i]);
  }
 
  for(var i in a) {
    alert(a[i]);
  }
]]>