You should not define methods directly in an instantiated object (by a constructor function), since on every new instance you get a new copy in the memory. Use JavaScript's prototype concept. You can override the prototype's methods in an object. It will be invoked by a call to obj.myFunction() or this.myFunction() from inside. If it not not found in the instance, it will be looked up in the prototype chain. You can build multiple prototypes in a chain and so you can build an inheritance model. When you use the Object.defineProperties method, you further can control whether a property (methods are executable properties as well) is writable or not and other settings.
What you are trying in your example and some other answers illustrate, is not overriding, but overwriting methods. When overwriting, the original method is lost.
MyClassPrototype = {};
Object.defineProperties(MyClassPrototype,
{
'someFunc':
{
value: function()
{
this.calledFunc();
}
},
'calledFunc':
{
value: function()
{
document.writeln('orig called<br>\n');
},
writable: true
}
});
function MyClass()
{
}
MyClass.prototype = MyClassPrototype;
var obj = new MyClass();
obj.someFunc();
obj.calledFunc = function()
{
document.writeln("not orig called AND...");
// call original method with proper this-context (instantiated object)
Object.getPrototypeOf(this).calledFunc.call(this);
}
obj.someFunc();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titel</title>
</head>
<body>
</body>
</html>