I have an app which uses a custom defined generic class which always has the properties type
and content
but can represent a variety of different content types and must define generic methods for each type such as getContent
, setContent
, etc. To do this, I've defined the generic class as so in one file:
function Block(type) {
this.type = type;
this.content = "";
switch(this.type){
case "text":
Object.setPrototypeOf(this, TextBlock);
break;
case "picture":
Object.setPrototypeOf(this, PictureBlock);
break
}
}
and the respective prototypes in their own files like so:
// textblock.prototype.js
TextBlock = {
createLine() {
//
},
setContent() {
//
},
//etc
}
When a new block is created with new Block("text");
the type, content, and prototype methods will be initiated. These prototype methods will only be set once with Object.setPrototypeOf()
and will not be changed or reset throughout the execution of the code.
My question is: does using Object.setPrototypeOf()
in this way significantly reduce performance? MDN has a warning on their page saying that using this method could have severe performance issues but I was wondering if anyone more knowledgeable on the subject could speak about when the appropriate use for this method would be. It is afterall a standard.