Let's take an example of a circle object. When I copy the properties of this object to RoundButton
with my custom extend
function, I thought RoundButton
will get the reference of area
method from circle
object.
So if I change the definition of area
method in circle
it will also change the area
property in RoundButton
but in the console, it remains as it was before. I am really confused if I have not cloned the properties but copied them why it is behaving like that.
var circle = {
area: function() {
return Math.PI * this.radius * this.radius;
}
};
function extend(destination, source) {
for( var k in source ){
if(source.hasOwnProperty(k)) {
destination[k] = source[k];
}
}
return destination;
}
var RoundButton = function (radius) {
this.radius = radius;
};
extend(RoundButton.prototype, circle);
console.log(RoundButton.prototype.area);
// ƒ () { return Math.PI * this.radius * this.radius;}
circle.area = function() {
return Math.PI * this.radius * this.radius * this.radius;
}
console.log(RoundButton.prototype.area);
// ƒ () { return Math.PI * this.radius * this.radius;}