Search code examples
javascriptobjectmethodstostringprototype-programming

how can I hide the default methods of Object when creating a new instance


I would like to hide all the default methods like toString, hasOwnProperty, valueOf etcetera when creating an instance. How is this possible?

function Foo() {};
var x = new Foo();
x.toString(); //should fail

And additionaly; I can understand that if I do this, I cannot do the following:

console.log(x);

But what else is using these functions?


Solution

  • If you really wanted to you could write over the toString method:

        Foo.prototype.toString = undefined;