O'Reilly logo

Ajax on Rails by Scott Raymond

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Core Extensions

Prototype’s core extensions are methods for working with JavaScript data structures, through new classes and extensions of core classes.

Array Extensions

The $A( object ) method converts object into an array. Combined with the extensions for the Array class, this makes it easier to convert or copy any enumerable list into an array. One suggested use is to convert DOM NodeLists into regular arrays, which can be traversed more efficiently.

clear()

Empties the array and returns itself.

[1, 2, 3].clear() // => []
compact()

Returns the array without the elements that are null or undefined. Does not change the array itself.

[1, null, 3].compact() // => [1, 3]
first()

Returns the first element of the array.

[1, 2, 3].first() // => 1
last()

Returns the last element of the array.

[1, 2, 3].last() // => 3
flatten()

Returns a flat, one-dimensional version of the array. Finds each of the array’s elements that are also arrays and includes its elements in the returned array, recursively.

[1, [2], 3].flatten() // => [1, 2, 3]
indexOf( value )

Returns the zero-based position of the given value if it is found in the array. Returns -1 if value is not found.

[1, 2, 3].indexOf(1) // => 0
inspect()

Return a string representation of the array and its elements.

[1, 2, 3].inspect() // => "[1, 2, 3]"
reverse([ inline ])

Returns the array in reverse sequence. If inline is omitted or true, the array itself will also be reversed; otherwise, it remains unchanged.

[1, 2, 3].reverse() // => [3, 2, 1] ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required