Because in a method call the this
argument is always (in sloppy mode) casted to an object. What you see is a String
object, which was produced from the "test"
primitive string value. The array on which you call your method is already an object, so nothing happens and you just get the array as before.
If you use strict mode, this cast doesn't happen:
String.prototype.test = function() {
"use strict";
console.log(this);
};
var str = 'test';
str.test(); // logs "test"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…