jquery - How to have one member of a javascript object refer to another member of same object? -


this question has answer here:

while defining object in javascript, there way member of object access value of member of same object? example if defining below (using jquery)

var x = $.extend({}, {   greeting: 'hello',   message: this.greeting+' world' });  alert(x.message); 

this above code alert undefined world. how make take value of greeting during definition. in case expected output hello world.

you can use properties:

var foo = {     greeting: 'hello',     message() {         return this.greeting+' world'     } };  console.log(foo.message); foo.greeting = 'hi'; console.log(foo.message); 

although i'm not sure how these interact $.extend

in older browsers, make message function, rather property getter


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -