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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You seem to be replacing the entire
prototypeobject forStringwith your object. I doubt that will even work, let alone be your intention.The
prototypeproperty is not writable, so assignments to that property silently fail (@Frédéric Hamidi).Using the regular syntax works, though: