Can you explain why a custom method on an array is not functioning properly in this scenario?
function runTestMethod() {
alert("method running");
}
String.prototype.testMethod = runTestMethod;
var A = "A";
A = A.testMethod(); // This works
var B = new Array();
B[0] = "food";
B[1] = "bar";
B = B.testMethod(); // This results in an error 'undefined' is not a function
B[0] = B[0].testMethod(); // This also throws an error 'undefined' is not a function
B[0] = B[0].slice(0,-1); // This works
UPDATE: The reason for this issue is that I am attempting to use a String.prototype on an array. My method should actually be Array.prototype instead. Even though array "B" contains string elements, they are still treated as array-object-properties rather than actual strings. The behavior of the slice() method is confusingly designed to work on both strings and arrays. Special thanks to T.J. Crowder for providing clarification on this matter.