JSFiddle - React, Tailwind, and code Playground
JavaScript
var fruitType = {
"apple": 0,
"orange": 1
};
var fruit = function(name) {
this.getName = function(){
return name;
}
this.setName = function(newName){
if (newName in fruitType) {
name = newName;
} else {
throw "wrong fruit type";
}
};
this.setName(name);
};
var f = new fruit("apple");
f.setName('orange'); // OK
try{
f.setName('toast'); // Throws an error
}catch(e){
console.log('Caught toast error: ' + e);
}
f.name = 'toast'; // This sets the `name` property to 'toast', but:
console.log(f.getName());