How to insert function value into js object. I try this construction (code with jQuery).

foo = $('.bar'); obj = { 0: function(){ if (foo[0].id){ return foo[0].id //Если у элемента есть id, то его надо вернуть } } } 

At the exit I get

 Object {0: function()} 

And it is necessary that the element id be in the property. How can this be done, please tell me.

  • I also forgot to say that I use such a construction in a loop, so I have to check id, because it may not exist and it will be necessary to leave the field empty. - Alex Pen
  • in js there is no definition for "leave the field empty". What do you understand by this? - Igor
  • this is not the essence of what to do with the field. The main thing in it to transfer foo.id - Alex Pen
  • What does the phrase "this is not the point" Is it in Russian? I know the word "syt": "Oh, you wolf syt, herbal bag ..." - Igor
  • one
    @Igor Essence - the most important and significant thing in Mr. - Darth

1 answer 1

 var obj = { 0: (function(){ ... })() }; 
  • Thanks for the answer. The noob question turned out, of course. But what to do, we all learn. - Alex Pen