I would like to define the structure of a class, and throw an error if the user attempts to set a property on one of my object instances using a property that is not defined in the class.
For example, say I have the following class:
class MyClass {
constructor() {
this.propertyA = 'A value';
this.propertyB = 'Another value';
}
}
And then when the user is modifying object instances...
let myInstance = new MyClass();
myInstance.propertyA = 'a new value'; // would work fine
myInstance.propertyC = 'unknown property value'; // throw exception
Is this possible? The seal keyword appears to be close to what I want. It would prevent new properties from being added, but I would like to throw exceptions in case the user type-o's the valid property names.
Update:
Using Object.preventExtensions
, Object.seal
, or Object.freeze
in strict mode will cause errors when a non-existent property is assigned to an object.
I would like to define the structure of a class, and throw an error if the user attempts to set a property on one of my object instances using a property that is not defined in the class.
For example, say I have the following class:
class MyClass {
constructor() {
this.propertyA = 'A value';
this.propertyB = 'Another value';
}
}
And then when the user is modifying object instances...
let myInstance = new MyClass();
myInstance.propertyA = 'a new value'; // would work fine
myInstance.propertyC = 'unknown property value'; // throw exception
Is this possible? The seal keyword appears to be close to what I want. It would prevent new properties from being added, but I would like to throw exceptions in case the user type-o's the valid property names.
Update:
Using Object.preventExtensions
, Object.seal
, or Object.freeze
in strict mode will cause errors when a non-existent property is assigned to an object.
- 1 Perhaps something like Typescript, which gives you static type checking and can issue warnings at pile time, is more useful than a runtime check here? – deceze ♦ Commented Aug 24, 2018 at 9:21
- You can make use of getters/setters – Rohit Goyal Commented Aug 24, 2018 at 9:21
- Related: Javascript : How to avoid addition of a new property in a function? – Cerbrus Commented Aug 24, 2018 at 9:24
1 Answer
Reset to default 10You can use a Proxy
to intercept new property additions and prevent new properties from being defined:
class MyClass {
constructor() {
this.propertyA = 'A value';
this.propertyB = 'Another value';
return new Proxy(this, {
get: (_, prop) => this[prop],
set: (_, prop, value) => {
if (!(prop in this)) throw new Error('Prop does not exist!');
this[prop] = value;
}
});
}
}
let myInstance = new MyClass();
myInstance.propertyA = 'a new value'; // would work fine
console.log('about to set propertyC:');
myInstance.propertyC = 'unknown property value'; // throw exception
A much terser method that prevents new properties from being added is to use Object.preventExtensions()
. Attempts to add new properties will throw an error in strict mode:
'use strict';
class MyClass {
constructor() {
this.propertyA = 'A value';
this.propertyB = 'Another value';
Object.preventExtensions(this);
}
}
let myInstance = new MyClass();
myInstance.propertyA = 'a new value';
console.log('About to add propertyC');
myInstance.propertyC = 'unknown property value';