最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Restrict javascript class instances to particular properties - Stack Overflow

programmeradmin1浏览0评论

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.

Share Improve this question edited Aug 24, 2018 at 17:01 benprime asked Aug 24, 2018 at 9:15 benprimebenprime 7610 bronze badges 3
  • 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
Add a ment  | 

1 Answer 1

Reset to default 10

You 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';

发布评论

评论列表(0)

  1. 暂无评论