This question is specifically about preventing unwanted properties from being added to a javascript "class". I have a class called Animal.
function Animal(){
this.name="";
this.type="";
//20 other properties
}
What would be the easiest way for a user to create their own Animal and add 20 properties. I want to also prevent the user from accidentally adding incorrect properties.
my current method:
var myAnimal= new Animal();
myAnimal.name="fluffy";
myAnimal.type="cat";
myAnimal.typo="cat";
//adding 20 more properties would require typing myAnimal 20 more times Plus if a user makes a typo it would add it as a new property.
I was hoping there would be something like this:
myAnimal=new Animal{
name:"fluffy",
type:"cat";
typo:"cat" //would throw an error
}
I've looked into Object.freeze Object.seal, Object.preventExtensions but not sure how they apply to classes.