2005-02-20

Javascript tip of the day

For all the annoying cases where javascript gives you an object with numeric indices but which isn't a proper Array object, you can upgrade it to become one using Array.prototype.slice.apply(object) It's more cruft than had the language already provided us with true arrays (in cases like the arguments variable bound in functions, the window.frames list and the various arrayish return values of numerous DOM methods, to name a few), but it's one less needless javascript level loop to do the transition.

And why doesn't javascript come with your basic set of higher level methods like mapcar and friends? Sure we can cook our own, but it's as if the language tries to imitate a blunt stone age axe, when it's capable of being a real power tool, with just a few touches here and there to get a proper class library. (Is this some misguided attempt at making the language more graspable to a web developer in diapers?)
Categories:

2 comments:

  1. where is the real tip ? Without at least a little example, this article is useless. Especially for non english readers

    ReplyDelete
  2. This is one of these tips you are not likely to need if you never run into the problem.

    If you have an Array-alike object (such as document.links) and want to apply some Array method to it, such as slice() to get a smaller specific subset of it, you need to upgrade the Array-alike object to a proper Array object, which can be done by way of Array.prototype.slice.apply( theobject ).

    And that was the whole tip.

    ReplyDelete

Limited HTML (such as <b>, <i>, <a>) is supported. (All comments are moderated by me amd rel=nofollow gets added to links -- to deter and weed out monetized spam.)

I would prefer not to have to do this as much as you do. Comments straying too far off the post topic often lost due to attention dilution.

Note: Only a member of this blog may post a comment.