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';
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 ?
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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With