javascript - Self-references in object literal declarations -


is there way following work in javascript?

var foo = {     a: 5,     b: 6,     c: this.a + this.b  // doesn't work }; 

in current form, code throws reference error since this doesn't refer foo. is there way have values in object literal's properties depend on other properties declared earlier?

well, thing can tell getters:

var foo = {   a: 5,   b: 6,   c () {     return this.a + this.b;   } };  foo.c; // 11 

this syntactic extension introduced ecmascript 5th edition specification, syntax supported modern browsers (including ie9).


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -