I was hoping to be able to augment Array.prototype with methods and then call them on any array:
>>> [1, 2, 3].customMethod();
But it appears arrays have no prototype…?
>>> [1, 2, 3].prototype
undefined
Am I missing something here?
It appears my actual problem lies elsewhere: calling [1, 2, 3].customMethod() works, but calling someDomElement.childNodes.customMethod() fails. Is childNodes not a real array?
childNodes.filter is not a function
prototypeis a property of constructor functions, likeArray. SoArray.prototypeexists, but not[1, 2, 3].prototype;Arrayis a constructor function, while[1, 2, 3]is an array.You are looking for
Object.getPrototypeOf([1, 2, 3]).Object.getPrototypeOfis an ECMAScript 5 method, and as such may not be present in all browsers. In which case, you can try accessing the__proto__property, i.e.[1, 2, 3].__proto__, which is an older, nonstandard thing thatObject.getPrototypeOfis the new standard version of, or you can use an ES5 shim to ensure that wherever__proto__is supported, so isObject.getPrototypeOf.