Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change value of a wrapped primitive data type

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

like image 280
Carlos Gil Avatar asked Nov 01 '10 01:11

Carlos Gil


People also ask

How do you convert primitive data into wrapper classes?

The automatic conversion of primitive data type into its corresponding wrapper class is known as autoboxing, for example, byte to Byte, char to Character, int to Integer, long to Long, float to Float, boolean to Boolean, double to Double, and short to Short.

What are primitive type wrappers?

So a primitive wrapper class is a wrapper class that encapsulates, hides or wraps data types from the eight primitive data types, so that these can be used to create instantiated objects with methods in another class or in other classes.

What is difference between primitive data type and wrapper classes?

One major difference from the explanation above is, default values of the primitive types depend on type like int is 0 , char is \u0000 , boolean is false etc but default value for wrapper classes of all types is null since they are objects.

Is string primitive or wrapper?

In JavaScript, a primitive (primitive value, primitive data type) is data that is not an object and has no methods or properties. There are 7 primitive data types: string.


2 Answers

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:

  • Object-to-Primitive Conversions
like image 112
Christian C. Salvadó Avatar answered Oct 03 '22 06:10

Christian C. Salvadó


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....

like image 45
hvgotcodes Avatar answered Oct 03 '22 07:10

hvgotcodes