I wrote this code in javascript:
String.prototype = {
a : function() {
alert('a');
}
};
var s = "s";
s.a();
I expect it alert an a
, but it reports:
s.a is not a function
Why?
You seem to be replacing the entire prototype
object for String
with your object. I doubt that will even work, let alone be your intention.
The prototype
property is not writable, so assignments to that property silently fail (@Frédéric Hamidi).
Using the regular syntax works, though:
String.prototype.a = function() {
alert('a');
};
var s = "s";
s.a();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With