Is it possible to access an object within itself?

Asked

Viewed 845 times

16

Cadastro = {
    "descricao" : "Novo usuário",
    "editando" : false
}

It is possible at runtime I get the value of the property by "editing" and use it?

Something like:

Cadastro = {
    "descricao" : "Novo usuário",
    "editando" : false,
    "titulo" : "editando" ? "Editar" : "Criar"
}

I found a way but it is not using the property but creating a variable in the global scope:

window.editando = true;
Cadastro = {
    "descricao" : "Novo usuário",
    "editando" : false,
    "titulo" : (window.editando ? "Editar" : "Novo")
}

Is there any other way to do this? What I want is when the variable value changes the return value "title" also changes.

4 answers

14


code, you can do as follows:

var Cadastro = function (descricao, editando) {
  this.descricao = descricao;
  this.editando = editando;
};

Object.defineProperty(Cadastro.prototype, "titulo", {
  get: function () { return this.editando ? "Editar" : "Criar"; },
  enumerable: true
});

var cadastro1 = new Cadastro("Novo usuário", false);
console.log(cadastro1.titulo); // Criar

var cadastro2 = new Cadastro();
cadastro2.descricao = "Novo perfil";
cadastro2.editando = true;
console.log(cadastro2.titulo); // Editar

Due to your implementation, you may encounter some difficulty because of the combination of Object.defineProperty and *.prototype, in this case define the property in the constructor of the "Class".

var Cadastro = function (descricao, editando) {
  this.descricao = descricao;
  this.editando = editando;

  Object.defineProperty(this, "titulo", this.descriptor.titulo);
};

Cadastro.prototype.descriptor = {};
Cadastro.prototype.descriptor.titulo = {
  get: function () { return this.editando ? "Editar" : "Criar"; },
  enumerable: true
}

var cadastro1 = new Cadastro("Novo Usuario", true);
var cadastro2 = new Cadastro("Novo Perfil", false);

console.log(JSON.stringify(cadastro1));
console.log(JSON.stringify(cadastro2));

  • 1

    I’m running some tests, but I believe your answer is correct for this situation.

  • 2

    Exactly what I wanted, thank you very much! =)

9

var Cadastro = {
    descricao: "Novo usuário",
    editando: false,
    get titulo() {
        if (this.editando) {
            return "Editar";
        } else {
            return "Criar";
        }
    }
};

console.log(Cadastro.titulo);
Cadastro.editando = true;
console.log(Cadastro.titulo);

6

With that with a little gambiarra it is possible yes.

Use a self-invoking Function making and use the this, so you will reference the object itself. I used the property __init to be able to refer to the object itself. Hence, as the variable this is only not accessible in the context of declaration, I call it at the end of the object declaration. So that variable Cadastro import the declaration value of our object, it is necessary to return the this at the end of that function.

See this as a "constructor" of our object.

Cadastro = {
    "descricao" : "Novo usuário",
    "editando" : false,
    "__init" : function ()
    {
          alert(this.editando)

          this.titulo = this.editando ? "Editar" : "Novo";
          return this;
    }
}.__init()

Check out on Jsfiddle

In that case, it is necessary that the this is within the context of the object you want to reference (in this case our anonymous function references Cadastro). If you use the this outside an object context, the reference will be window.

Another way to make that statement you need would be to declare that index titulo after declaring the object. Do so:

Cadastro = {
    "descricao" : "Novo usuário",
    "editando" : false,
}

Cadastro.titulo = Cadastro.editando ? "Editar" : "Novo";
  • Is it possible to call title property if it executes the function and recheck the value of the variable? I managed by taking out self-invoking and calling directly

  • If you used the this and it worked out, it’s because he’s in window. this outside a window reference scope. As your example you have created a property called editando in window, the this will take that amount from there.

  • Unfortunately the first way you quoted does not work properly: http://i.imgur.com/upB4Muh.png

  • I’m checking here, hold on...

  • @c0de with the new change works, see

  • You created a property that is an anonymous function taking the value of the current context right? :)

  • @Wallacemaxters, what happens to the property titulo if the property editando is changed after object initialization?

  • Yes.... When you said the this did not work is because we were calling the object in the context of statement. But as I call it in the end, so it is already "contact" as stated

  • 1

    @Tobymosque I understood where you want to go, but what the AP has made clear is that he wants to declare an object, already with the determined values, according to this property. So there’s no need for something more complex

  • In fact it is for a registration page the default is false, when I set to true I want it to return the treated string, but we are on the way.

  • @Wallacemaxters Take a look at the answer I added! :)

Show 6 more comments

1

The simplest and practical way:

Cadastro = {
    descricao: "Novo usuário",
    editando: false,
    titulo: function () {
        return this.editando ? "Editar" : "Novo";
    }
}

// Depois é so chamar quando precisar
console.log(Cadastro.titulo());
Cadastro.editando = true;
console.log(Cadastro.titulo());

  • It’s not practical because I have to call the method () and not just the property.

  • My young man the method ends up becoming the property itself, for it is defined from a condition.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.