I use javascript prototypal inheritance where A "inherits" B. B uses defineProperty
to define a setter for property prop
. In A I want to override this behaviour:
Function.prototype.inherits = function (parent)
{
this.prototype = Object.create(parent.prototype);
this.prototype.constructor = parent;
};
// --------------------------------------------
var B = function()
{
this.myProp = 0;
};
Object.defineProperty( B.prototype
, 'prop'
, {
set: function(val)
{
this.myProp = val;
}
});
// --------------------------------------------
var A = function(){};
A.inherits(B);
Object.defineProperty( A.prototype
, 'prop'
, {
set: function(val)
{
// Do some custom code...
// call base implementation
B.prototype.prop = val; // Does not work!
}
});
// --------------------------------------------
var myObj = new A();
myObj.prop = 10;
Calling the base implementation does not work this way as the this
pointer will be wrong. I would need to call something like B.prototype.prop.set.call(this, val);
to fix it but this does not work.
Would be greatful about any ideas!
EDIT: As desired I added some more code.