I understand that is question may have been touched on in other Q&A's (such as the How does JavaScript .prototype work? question), but (I hope!) this is a more specific question around WHY some "overrides" using .prototype
work in some scenarios, and some do not. To help illustrate (from an example at ejohn.org):
function Ninja() {
this.swingSword = function() { return true; };
};
Ninja.prototype.swingSword = function() { return false; };
var my_ninja = new Ninja();
alert(my_ninja.swingSword());
The above example will alert "true", which makes sense according to the ejohn.org website's example and explanation, but then subsequently this works too, completely contradicting what I thought was impossible based on the above code:
var _send = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function(){
alert("Overridden!");
_send.apply(this, arguments);
};
How can both hold true? One is accurately not being overridden, the other is? They are both Objects as far as I can tell, and the swingSword
and send
functions are both, well, functions? So how can we override the XMLHttpRequest
's send
function, but not the Ninja
's swingSword
function?
Thank you in advance for any insight and assistance!