How can I use dynamic Javascript properties so Foo.Bar returns the current date ()?

I want to create a property of an object that recalculates its value every time it is called.

I took a hit on him:

var Foo = { Bar : (function() { return Date(); })() }

alert(Foo.Bar); // shows time at object literal Foo was init'd
                // but need it to show time when it called

      

Is it even possible?

0


a source to share


1 answer


Impossible - JS properties are not "executable" like C # properties. The best you can do is the usual method.



Also: from a design point of view, I would consider the self-modifying property to be pretty poor. The principle of least surprise will be better served by the method anyway.

+5


a source







All Articles