Dynamic property of a JavaScript object?

I am wondering if this is possible in JavaScript, I want to have an object that can 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 a way to intercept property calls in JavaScript so that I can proactively set the no-value property to null?

Thanks.

+4
source share
8 answers

This is about as close as you can to your goal.

the 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"
+4
source

Proxy can do it

 var myobj = new Object(); var handler = { get:function (obj, name, proxyed){ if(obj[name] !== undefined) // if obj[name] exist return obj[name]; // then return obj[name] return null; // if obj[name] is not exist then return null; } }; var obj = new Proxy(myobj, handler); obj.property1 = "something"; alert(myobj.property1); // something alert(myobj.property2); // undefined alert(obj.property1); // something alert(obj.property2); // null 
+3
source

Yes, but only in version 2.0 and higher. The exact syntax is still TBD, but it looks like get * () {...} for object literals at least.

+2
source

Nope. JavaScript is not Smalltalk.

+1
source

Cannot intercept direct access to properties in JavaScript. When a property is restored that has not been set, the result will be undefined . Although null and undefined are usually considered the same, they are actually different objects.

In JavaScript, undefined does not have a value, and null means null. In some cases, you can mix undefined and null. For example, when using the == operator, they are equivalent ( (null == undefined) === true ). Using the operator without coercion, === , they are different ( (null === undefined) === false ).

You can take advantage of this. While most people claim that you should use the force equality operator ( === ), it’s basically safe to put null and undefined in the same bucket, especially since you really care about the difference between the two, Where it becomes it is difficult that undefined is a property of a global object and therefore a new value can be assigned.

If someone says undefined = 'donkey' , then null == undefined will start returning false . In practice, this is almost never a problem, since most people are not stupid enough to reassign an undefined value.

This way you do not need to intercept access to properties to return null for properties that were not set while you compare the result with null with == ./p>

+1
source

No, if you are not manipulating an object controlled by the NPAPI plugin, then you can implement the intended behavior.

In other words, through the NPAPI plugin, you can implement the behavior you are looking for.

0
source

Check out javascript prototypes. I think this will give you at least part of what you are looking for. Just google up the "javascript prototype".

0
source

In your example, the second warning will not generate an error. It will simply warn undefined . Accessing property properties will result in an error:

 myobj.property2.property3 
0
source

All Articles