9
var test = new Boolean(0)
test.prop = "OK!"

Can you change the value of test to true?

but test.prop should still be "OK!"

in other words, test should be the same object

3 Answers 3

11

Built-in object wrappers (created with the Boolean, Number, String and Date constuctors) store the primitive wrapped value in an internal property named [[PrimitiveValue]], which cannot be changed, but...

You can override the valueOf method of your test object:

var test = new Boolean(0);
test.prop = "OK!"
// override valueOf:
test.valueOf = function () { return true; };

if (test == true) { // using the equals operator explicitly for type conversion
  alert(test.prop); //"OK!"
}

It will work since the valueOf method is used internally by the type-conversion mechanisms triggered by the equals operator.

When one of the operands is a Boolean value, both are converted at the end to Number.

If we don't use the equals operator (e.g. if (test) { ... }), since test is an object, when converted directly to Boolean, it will always yield true.

Any object converted to Boolean will produce the true value, the only values that can produce a false result, are the "falsey" values (null, undefined, 0, NaN, an empty string, and of course the false value), anything else will produce true.

More info:

3
  • although the string representation remains to false, since the underlying value has not really changed .. Nov 1, 2010 at 1:43
  • 2
    @Gaby, well, it depends how the type conversion is made, for example if you concatenate a string to it, e.g. test+'' the result still will be 'true' (valueOf will be used first), if you use the String constructor as a function, e.g.: String(test); it will call the Boolean.prototype.toString method. I agree, it's a good idea to override toString also. Nov 1, 2010 at 1:49
  • ah very nice.. i just alerted the value alert(test) and it showed false. Looks like the alert method uses the String constructor or calls toString on its arguments.. thanks for the heads up. Nov 1, 2010 at 9:09
1

No.

var test = new Boolean(0);

creates a new Object and puts a reference to it in 'test'

test.prop = 'ss'

dynamically puts a new property on the object (meta-programming FTW!)

test = true;

assigns test to a boolean primitive type. The reference to the Boolean object is lost.

Finally, the Boolean object does not appear to have any methods to toggle or reassign its value, i.e. Boolean objects are immutable.

However, you could add a method to Boolean.prototype to change that if you wanted....

0

The object wrappers create an immutable reference type in JS. Since in JS the primitive data types are immutable, their references should also be immutable. It might be useful for some rare cases but most probably will fail to do what you want. For instance, if you need to alter the source primitive value and watch the references to follow it, you can't use the Object constructor to achieve the desired effect. Then your best bet would be to avoid the Object constructor and use a proper object literal to harbor your primitive value under a property as follows;

var pv = "test",
   pvr = {0: pv},
   arr = new Array(5).fill().map(_ => pvr);
console.log(JSON.stringify(arr));
pvr[0] = "Hello";
console.log(JSON.stringify(arr));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.