[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Mutating the [[Prototype]] of an object

Richard Maher

4/18/2015 6:58:00 AM

Hi,

I found this warning in the FireFox debugger today: -

"mutating the [[Prototype]] of an object will cause your code to run
very slowly; instead create the object with the correct initial
[[Prototype]] value using Object.create"

It's complaining about the code below (which, to be honest, is not
really necessary and there are other ways to skin this particular cat).

How much more slowly will it make the code run? Is "mutating the
prototype" really such a crime?

The requirement behind it is sometimes (user selected) I have a Circle
and sometimes I have a Rectangle overlayed on a google map. Now, I don't
want a whole lot of conditional logic so I sought to put a Rectangle peg
in a Circle hole. (See what I did there :-)

Anyway I thought it useful to homogenize all functionality in to a
radarView which could inherit from either circle or rectangle after they
forked off google.maps.MVCObject


function radarView(superBase)
{
this.__proto__ = superBase;

if (this instanceof google.maps.Circle) {
augmentCircle.apply(this);
} else if (this instanceof google.maps.Rectangle) {
augmentRectangle.apply(this);
} else {
Toolbox.reportError({header:"Internal error",
message:"Inheriting from unknown object"});
}

this.doX = function(x){return "x is>" + x;};

return this;
}

function augmentCircle()
{
this.moveBorder = function()
{
google.maps.event.trigger(radarCircle,"center_changed");
}
}

function augmentRectangle()
{
this.moveBorder = function()
{
google.maps.event.trigger(radarRectangle,"boundsChanged");
}
}