4

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.

2
  • 3
    Why do you explicitly want it to be "null"? Doesn't "undefined" suffice? Commented Nov 18, 2009 at 0:30
  • I thought my application doesn't take undefined value, I just tried, undefined does the same job, so I guess this can be closed now. Thanks everyone. Commented Nov 18, 2009 at 0:50

8 Answers 8

4

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"
Sign up to request clarification or add additional context in comments.

Comments

4

'Proxy' can do that

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

Comments

2

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

2 Comments

Some source URL for this would be really helpful. I’ve so far only found the specification for direct proxies in ES.next which allow what the OP was looking for.
He asked about JS, not ES. Anyways, JS 2.0 is pretty much canceled and ES.next is what browsers will be implementing.
1

Nope. JavaScript is not Smalltalk.

1 Comment

Then, i guess that I should do something like this: MyObject.prototype.getProperty = function(name) { if (this.hasOwnProperty(name)) return this[name]; else return null; } Then, I will use myobj.getProperty(propertyName) when I need to access it. Looks like this is the best option.
1

There is no way to intercept direct property accesses in JavaScript. When a property is retrieved that hasn't been set than the result will be undefined. Although null and undefined are usually considered to be the same thing they are in fact different entities.

In JavaScript undefined means no value and null means a value of null. In some cases you can mix undefined and null. For example, when using the == operator they are equivalent ((null == undefined) === true). Using the non-coercing operator, ===, they are different ((null === undefined) === false).

You can use this to your advantage. While most people will claim that you should use the non-coercing equality operator (===) it's mostly safe to put null and undefined in the same bucket, in less of course you actually care about the difference between the two. Where it gets tricky is that undefined is a property of the global object and can therefore be assigned a new value.

If someone were to say undefined = 'donkey' then null == undefined would start to return false. In practice this is almost never a problem since most people aren't foolish enough to reassign the value of undefined.

So, in a roundabout sort of way, you don't need to trap property accesses to return null for properties that have not been set so long as you compare the result against null using ==.

Comments

0

No unless you are manipulating an object controlled by an NPAPI plugin in which case you could implement the intended behavior.

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

Comments

0

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

Comments

0

In your example the second alert will not generate an error. It will just alert undefined. Accessing properties of properties will generate an error:

myobj.property2.property3

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.