I am wondering if this is possible in JavaScript, I want to have an Object which could contain dynamic properties.
Give an example:
function MyObject()
{
}
var myobj = new MyObject();
myobj.property1 = "something";
alert(myobj.property1); // something
alert(myobj.property2); // this should never report error, instead the property should be evaluated to null, as it has never been set.
Is there any way to intercept property calls in JavaScript so I can proactively set a no-value property as null?
Thanks.
This is about as close as you can get to achieving your goal.
Code:
var obj = {};
alert("prop: " + obj.prop);
obj.prop = "something";
alert("prop: " + obj.prop);
delete obj.prop;
alert("prop: " + obj.prop);
Behavior:
Alert: "prop: undefined" Alert: "prop: something" Alert: "prop: undefined"
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