faqts : Computers : Programming : Languages : JavaScript : Language Core : Arrays

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

20 of 21 people (95%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

How do you prototype Array to add a function like push?

Jan 19th, 2001 08:55
acebone, Martin Honnen,


Here is an example

if (window.Array && Array.prototype && !Array.prototype.push)
  Array.prototype.push = _Array_push;
function _Array_push () {
  for (var a = 0; a < arguments.length; a++)
    this[this.length] = arguments[a];
  return this.length;
}
var a = ['Kibo', 'Maho', 'Xibo'];
a.push('Marco', 'Jaffo');
alert(a)

===========================================================

see:
    http://www.faqts.com/knowledge_base/view.phtml/aid/3838/fid/144

for a complete implementation of array functions
not supported by Internet Explorer