5
function Shape(X,Y) {
    this.X = X;
    this.Y= Y;
}

function Rectangle(Name,Desc,X,Y) {
    Shape.call(this, X, Y);
    this.Name = Name;
    this.Desc = Desc;
}

var Z = new Rectangle('Rectangle', '',25,25);
Z.ABC = '123';

enter image description here

The problem is, the Z.ABC is not the variable under the function Shape and Rectangle, it should hit error because ABC is not the variable under shape and rectangle function.

How to disable unknown variable, not allow to declare unknown variable outside the function ?

Howard Teoh
  • 75
  • 3
  • 8
  • I currently have no idea what you mean. Variables aren't disabled. If you want to *delete* it, you can, via `delete Z.ABC`. (Note, it's a property, not a variable.) – Mitya May 12 '18 at 14:17
  • you could use a proxy and omit setting. – Nina Scholz May 12 '18 at 14:17
  • 1
    It sounds like you're asking for some static type checking that prevents the addition of properties that are not defined in the constructors? –  May 12 '18 at 14:18
  • 4
    They're called object *properties* in JavaScript, not "variables". – Bergi May 12 '18 at 14:22
  • What case are you trying to protect yourself from by doing this? – Paul S. May 12 '18 at 14:23
  • 1
    This is useful for protection against bugs from typos or simply imperfect recall of an API. Also promotes efficiency in modern engines that need to create a new underlying class with each new extension. –  May 12 '18 at 14:26
  • I do think that a static type checker like Flow or TypeScript would be more desirable than runtime checks. –  May 12 '18 at 14:32

2 Answers2

5

You can call Object.preventExtensions on your object after constructing it and adding all the properties that you want. You won't be able to create new properties on the object then.

"use strict";
function Shape(X,Y) {
    this.X = X;
    this.Y= Y;
}

function Rectangle(Name,Desc,X,Y) {
    Shape.call(this, X, Y);
    this.Name = Name;
    this.Desc = Desc;
}

var Z = Object.preventExtensions(new Rectangle('Rectangle', '',25,25));
Z.ABC = '123'; // Uncaught TypeError: Cannot add property ABC, object is not extensible
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Wow, I think I may actually remember reading about that once, many years ago. Nice find/recall/suggestion. –  May 12 '18 at 14:20
  • Unfortunately this prevents mutation of primitives that are inherited, say like default values defined only on a `.prototype`. Maybe not a concern for the OP though. –  May 12 '18 at 14:36
  • solution found : more easy solution is add Object.seal(this); into the class. – Howard Teoh May 19 '18 at 01:41
  • @HowardTeoh You can put the `Object.preventExtensions(this)` in the constructor as well. You don't need to `seal` your object for that (make all properties-nonconfigurable in addition to preventing extensions). – Bergi May 19 '18 at 10:48
1

To prevent assignment operations to your objects you can "freeze" them. Reference

function Shape(X,Y) {
    this.X = X;
    this.Y= Y;
}

function Rectangle(Name,Desc,X,Y) {
    Shape.call(this, X, Y);
    this.Name = Name;
    this.Desc = Desc;
}

var Z = new Rectangle('Rectangle', '',25,25);
Object.freeze(Z);
Z.ABC = '123';
console.log(Z);
Randy Casburn
  • 13,840
  • 1
  • 16
  • 31
  • 2
    I don't think the OP wants to freeze all mutation; just undeclared properties. But I may be wrong. –  May 12 '18 at 14:19