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?)
where is the real tip ? Without at least a little example, this article is useless. Especially for non english readers
ReplyDeleteThis is one of these tips you are not likely to need if you never run into the problem.
ReplyDeleteIf you have an Array-alike object (such as
document.links
) and want to apply some Array method to it, such asslice()
to get a smaller specific subset of it, you need to upgrade the Array-alike object to a properArray
object, which can be done by way ofArray.prototype.slice.apply(
theobject)
.And that was the whole tip.