Is it possible to give an object a dynamic property, or rather give an object property a dynamic value? For example:
var obj = {
a: 0,
b: 5,
c: //should always be a+b
}
Obviously I could use a method c=function(){return a+b}
, but then I would always need to call c
as a method using braces (obj.c()
instead of obj.c
). This wouldn't really be a problem, but I belive it has to work somehow, since the build in length
property also is a property and not a function. This property changes dynamically with the object and isn't a function...
var str = "hello world";
console.log(str.length); //11
str = "hello stack overflow";
console.log(str.length); //20
The property value changes without it beeing updated...
I hope you can help me :)