Comments on: Library foundation code https://j11y.io/snippets/library-foundation-code/ Sun, 22 Mar 2015 15:39:22 +0000 hourly 1 https://wordpress.org/?v=5.0.13 By: Orfebre https://j11y.io/snippets/library-foundation-code/#comment-1918 Wed, 25 Aug 2010 00:29:24 +0000 https://j11y.io/?p=1527#comment-1918 We can do better than that, by feature testing at load-time if Array.prototype.slice/push can convert nodelist to array

]]>
By: Nick Tulett https://j11y.io/snippets/library-foundation-code/#comment-1917 Wed, 12 May 2010 20:42:50 +0000 https://j11y.io/?p=1527#comment-1917 or even

for (var i = 0, per; per=this[i++];) {
    fn.call(per, per, i, l, this);
}

Probably best to stop now…

]]>
By: Nick Tulett https://j11y.io/snippets/library-foundation-code/#comment-1916 Wed, 12 May 2010 17:02:35 +0000 https://j11y.io/?p=1527#comment-1916 Interesting for loop construction

for (var i = -1, l = this.length; ++i < l;) {
                fn.call(this[i], this[i], i, l, this);
            }

What are your views on the nano-optimisation possibilities of something like:

for (var i = 0, per; per=this[i]; i++;) {
                fn.call(per, per, i, l, this);
            }
]]>
By: steve https://j11y.io/snippets/library-foundation-code/#comment-1915 Wed, 28 Apr 2010 03:55:47 +0000 https://j11y.io/?p=1527#comment-1915 Very nice work. Useful wrapping tutorial

]]>
By: James https://j11y.io/snippets/library-foundation-code/#comment-1914 Tue, 27 Apr 2010 07:51:07 +0000 https://j11y.io/?p=1527#comment-1914 Thanks for your commments!

@Jeff, I’ve added some comments to the code. Good point 🙂

@kangax, I’ve modified it as per your recommendations. It seems to work… I didn’t bother with canConvertNodelistToArray — I just assumed if it hasn’t thrown an exception within the try{} then I can continue to return the slice() function:

try {
 
    slice.call(document.childNodes);
 
    return function(arrayLike) {
        return slice.call(arrayLike);
    };
 
} catch(e) {}
]]>
By: kangax https://j11y.io/snippets/library-foundation-code/#comment-1913 Tue, 27 Apr 2010 04:43:55 +0000 https://j11y.io/?p=1527#comment-1913 Looks like myWrapper is created through undeclared assignment instead of being declared properly. You must have missed it?

Converting nodelist to array with help of try-catch is a rather crude approach. We can do better than that, by feature testing at load-time if Array.prototype.slice/push can convert nodelist to array. If you care about performance, you can fork toArray (or whatever it’s called) method at load-time as well.

Something along these lines (untested, but hopefully shows the point):

var toArray = (function(){
 
  var canConvertNodelistToArray = false, 
      slice = Array.prototype.slice,
      arr;
 
  try {
    arr = slice.call(document.childNodes, 0);
    canConvertNodelistToArray = arr instanceof Array;
  } 
  catch(err) { }
 
  if (canConvertNodelistToArray) {
    return function(arrayLike) {
      return slice.call(arrayLike);
    };
  }
  return function(arrayLike) {
    /* custom conversion */
  };
})();
]]>
By: Jeff https://j11y.io/snippets/library-foundation-code/#comment-1912 Tue, 27 Apr 2010 00:40:18 +0000 https://j11y.io/?p=1527#comment-1912 Thanks for the post. It’s pretty informative for those who look under the hoods of libraries or like you said, those would would like to start something for themselves. Even though it’s just a foundation, I just wish you could of commented a little more to explain more of the what and why.

]]>