Why is a custom method on an array not working here:
function runTestMethod() {
alert("method running");
}
String.prototype.testMethod = runTestMethod;
var A = "A";
A = A.testMethod(); // WORKS
var B = new Array();
B[0] = "food";
B[1] = "bar";
B = B.testMethod(); // ERROR 'undefined' IS NOT A FUNCTION
B[0] = B[0].testMethod(); // ERROR 'undefined' IS NOT A FUNCTION
B[0] = B[0].slice(0,-1); // WORKS
UPDATE: the answer is that I'm trying to use a String.prototype on an array. My method needs to be Array.prototype instead. Despite array "B" containing string members, these are still being treated as array-object-properties not actual strings. The factory method of slice() is confusingly designed to work on both strings and arrays. Thanks to T.J. Crowder for the explanation.
Bis an array containing entries. The entries in the array (in this case) are strings.sliceisn't "designed to work on both strings and arrays," they just each have a function by that name (different functions that do similar, but different, things).