Is it possible to access the property an object inside itself?

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

Is it possible at runtime to get the property value "editing" and use it?

Something like:

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

I have 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 value of the variable change the value of the return "title" also changes.

    
asked by anonymous 16.05.2016 / 14:15

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

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

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));
    
16.05.2016 / 15:06
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);
    
16.05.2016 / 16:47
6

With a little gambiarra you can.

Use a self-invoking function by doing and using this , so you'll reference the object itself. I used the __init property to refer to the object itself. Hence, since the variable this is not accessible only in the declaration context, I call it at the end of the declaration of the object. In order for the variable Cadastro to import the declaration value of our object, it is necessary to return this to the end of this function.

See this as a "builder" 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 JSFiddle

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

Another way to make this statement you need would be to declare this index titulo after declaring the object. Here's how:

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

Cadastro.titulo = Cadastro.editando ? "Editar" : "Novo";
    
16.05.2016 / 14:24
1

The simplest and most 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());
    
20.05.2016 / 20:38