Given:
(function() {
function Foo() {
}
$.extend(Foo.prototype, {
bar: 'hasBeer'
});
})
Is it possible to change the bar method from outside of that closure?
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.
If you have access to the constructor function (
Foo) and you want to overridebarfor all instances, you assign a new value toFoo.prototype.bar.If you have an instance of
Foo, you can either only overridebarfor that instance:or for all instances by, again, overriding the prototype method. For this you have to get the prototype first, which you can do with
Object.getPrototypeOf[MDN]:But note that this is an ES5 method and is not available IE <= 8 or Opera.
If you neither have access to the constructor, nor an instance, you cannot change the property, other than by modifying the source code.