I'm trying to create an object where its methods can not be changed. That is, its value, even if changed, should continue as initially defined.
Example:
var object = (function(object){
object.method = function (text) {
return 'response ' + text + '!!!';
}
})({});
I can access it as follows:
object.method()
But you can change the method by simply doing this:
object.method = null
//TypeError: object is not a function
In jQuery
, when we make this attempt to change, the html method continues to do the same thing for which it was designed.
$('body').html = null
$('body').html() // funciona normalmente
Unless you save the value of $('body')
to a variable:
$body = $('body');
$body.html = null;
////TypeError: object is not a function
In the first example used with jQuery
, how does it internally do to keep the methods intact? Does it always create a new instance?
How to block rewriting of an object's methods?