x.field = true;
x.field.netType = "System.Boolean";
is actually working.
x.field
which is a primitive boolean value is getting converted to object internally but we dont have a reference of it so immediately it becomes garbage. If we store the refrence of x.field
so that it doesnt be garbage we can get the value. like this....
x.field = true;
var y = x.field.netType = "System.Boolean";
alert(y);
If you write you code like this
var x = {};
x.field = {};
x.field.netType = "System.Boolean";
alert(x.field.netType);
Then it will work.
In your code this line x.field.netType = "System.Boolean";
will throw error in `strict mode
`//Cannot assign to read only property 'netType' of true`
Why this line x.field.netType
gives undefined
Objects of this type are merely wrappers, their value is the primitive they wrap and they will generally coerce down to this value as required.
JavaScript will readily coerce between primitives and objects
.
var a = 'Intekhab';
a.length;//In this case the string value is coerced to a string object in order to access the property length.
var Twelve = new Number(12);
var fifteen = Twelve + 3; // In this case Object Twelve is coerced to a primitive value.
fifteen; //15
If JavaScript detects an attempt to assign a property to a primitive it will indeed coerce the primitive to an object. This new object has no references and will immediately become fodder for garbage collection.
var primitive = "september";
primitive.vowels = 3;
//new object created to set property
(new String("september")).vowels = 3;
primitive.vowels;
//another new object created to retrieve property
(new String("september")).vowels; //undefined