0

I'm trying to create an object like this:

var DadosUtente = true;
var DevolucaoModelo = {
get UtNome() { return (DadosUtente) ? $("#UT_Nome") : $("#Equipamento_Nome")}
};

If i change the DadosUtente the selector returned changes too.

This works properly in Chrome, but when i've tested it with Internet Explorer i get an error because the browsers is excepting :

In the rest of code i'm accessing the selectors like DevolucaoModelo.UtNome.val();

Can someone help me?

By the way, i've search a lot in google and tried others solutions but without success in IE.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Gui
  • 9,555
  • 10
  • 42
  • 54

1 Answers1

3

Why not try:

var DadosUtente = true;
var DevolucaoModelo = {
    getUtNome : function() { return (DadosUtente) ? $("#UT_Nome") : $("#Equipamento_Nome")}
};

Update

If you don't want to call a function then do it like this:

var DadosUtente = true;
var DevolucaoModelo = {
    UtNome : (DadosUtente) ? $("#UT_Nome") : $("#Equipamento_Nome")
};

Then call it like DevolucaoModelo.UtNome.

Community
  • 1
  • 1
Shef
  • 44,808
  • 15
  • 79
  • 90
  • Because i was looking for an alternative solution that doesn't involve to call an function – Gui Jul 15 '11 at 18:18
  • @guilhermeGeek: Accessors and mutators are functions. Just use a function. It'll save you grief, and there's no reason not to. – Lightness Races in Orbit Jul 15 '11 at 18:28
  • Shef that way doesn't work because if you change the "DadosUtente" to false, the object will keep returning the $("#UT_Nome") anyway. I'll use functions instead, thanks ;) – Gui Jul 15 '11 at 18:34
  • @guilhermeGeek: If it should change after the first execution, of course you need the functions. – Shef Jul 15 '11 at 18:36
  • 1
    Yes mate it changes after the first execution. I guess i'm too used to C#, i really need to know better JS. Thank you all the the replies ;) – Gui Jul 15 '11 at 18:39