Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WebReflection/9669354 to your computer and use it in GitHub Desktop.
Save WebReflection/9669354 to your computer and use it in GitHub Desktop.
some example about define-strict-properties - https://github.com/WebReflection/define-strict-properties

All code assumes define strict properties in included upfront.

However, being the proposal fullyt backward compatible, you can tests these snipets by copy and paste but no type will be guarded.

Point2D Class

function Point2D(x, y) {
  this.x = x;
  this.y = y;
}
Object.defineProperties(
  Point2D.prototype,
  {
    x: {
      type: 'number',
      writable: true
    },
    y: {
      type: 'number',
      writable: true
    }
  }
);

var p = new Point2D(12, 3);
p.x; // 12
p.y; // 3

p.x = '4'; // throws Error: expected number
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment