-1

I'd like to validate property values for when do var obj = new Object(value); and when i do obj.value = newValue. In the methods I've employed below I seem to be able to get one or the other to work, but not both. Is there a way to get both to work in the same object declaration?

In the snippet below I'd like to receive Boolean values only, so I've said "if value received is not Boolean then just assign true.

/*
* why does this work on instantiation but not on property assignment?
*
*/

var robot = function(isAlive) {
    this.isAlive = (typeof isAlive === 'boolean') ? isAlive : true; // why does this work on instantiation but not on property assignment?
};

bot4 = new robot(true);
bot5 = new robot("random string");

bot4.isAlive = "random string";
console.log("bot4: " + bot4.isAlive); // -> random string
console.log("bot5: " + bot5.isAlive); // -> true



/*
* why does this work on property assignment but not on instantiation?
*
*/

var android = function(isAlive) {
  Object.defineProperty(this, "isAlive", {
    get: function() {
      return isAlive;
    },
    set: function(value) {
      isAlive = (typeof value === 'boolean') ? value : true; // why does this work on property assignment but not on instantiation?
    }
  });
};

droid1 = new android(true);
droid2 = new android("random string");

droid1.isAlive = "random string"; // note the string assignment failed and is assigned the default value of true
console.log("droid1: " + droid1.isAlive); // -> true

droid1.isAlive = false; // passed since this is boolean
console.log("droid1: " + droid1.isAlive); // -> false

console.log("droid2: " + droid2.isAlive); // -> random string

View on JSFiddle

2 Answers 2

1

To get both working just set the property in the constructor after you have defined it like so:

var android = function(isAlive) {
  Object.defineProperty(this, "isAlive", {
    get: function() {
      return isAlive;
    },
    set: function(value) {
      isAlive = (typeof value === 'boolean') ? value : true; // why does this work on property assignment but not on instantiation?
    }
  });

  this.isAlive = isAlive;
};

JsFiddle

Sign up to request clarification or add additional context in comments.

Comments

0

The first one doesn't work because assigning to the "isAlive" property of an instantiated object simply performs that assignment. The code in the constructor only runs when you call the constructor.

The code in the second example works fine. The problem is that your setter only accepts boolean values. If you set the "isAlive" property to a string, the code in your setter sets the value to true.

The key is that the "isAlive" parameter to your constructor function is a completely different thing that the "isAlive" property of the constructed object.

2 Comments

I am aware of what's happening, what I'm trying to find out is if there is a way to have both; i.e. validate both on instantiation as well as assignment.
@komplexb The other answer will do what you want.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.