How to change a value of a literal Javascript object?

Asked

Viewed 1,096 times

1

I am trying to change the value of a property in a Javascript object, but in the creation of the object literally, and not constructor.

var cliente = {
  nome: "Wesley",
  idade: 20,
  cargo: "Front End",
  setAtualiza : functiion(n, i, c){
    this.nome = n;
    this.idade = i;
    this.cargo = c
  }
};

setAtualiza("NovoNome", 25, "Pleno");
for(var x in cliente){
  console.log(cliente[x]);
};

setAtualiza();

And the exit I’d like to have is: "Novonome 25 Full"

What am I doing wrong? Or is it not possible to do (update a literal object).

  • Where the setAtualize function?

3 answers

6


In addition to a typo you are calling a function that does not exist globally, it only exists in the context of the client object, so you must call it through the object.

var cliente = {
    nome: "Wesley",
    idade: 20,
    cargo: "Front End",
    setAtualiza : function(n, i, c) { //tinha um erro de digitação aqui.
        this.nome = n;
        this.idade = i;
        this.cargo = c
    }
};

cliente.setAtualiza("NovoNome", 25, "Pleno"); //a chamada deve ser contextual
for (var x in cliente) {
    console.log(cliente[x]); //mudei aqui só executar certo no SO, pode manter o seu
};

I put in the Github for future reference.

  • In fact, it did work! The problem is that it is also showing the whole 'setAtualize' function. That is, the ideal is to present only the first three values, leaving aside the function. Imagine if I have one more function there, it would return it too, right? Please... Almost there!

  • This is another question. Listing the members of an object serves a few practical things. Or you have to filter in hand what you want or use some non-standard artifices for this filter to occur automatically. None is ideal. I won’t go into detail because this is another matter.

  • I understand. In your practices, then, what is the best definition to update the values of an object: a local function or a global function? I did with a global function here and it worked correctly.

  • 1

    For this type of use I think the local function is better to the object, but I don’t know if this is idiomatic in JS, I would give this preference in most cases. The problem there is trying to list all the members, rarely this is desirable except in tests and checks, which I thought was why I had made this code. List the members I believe are not idiomatic any language since each context you may want to do in a way, so you can not get value of the object structure.

5

Function is misspelled and to call a method you need a reference to the object in question. In this case this reference can be accessed through the client variable. It follows modified code block:

var cliente = {
  nome: "Wesley",
  idade: 20,
  cargo: "Front End",
  setAtualiza : function(n, i, c){
    this.nome = n;
    this.idade = i;
    this.cargo = c
  }
};

cliente.setAtualiza("NovoNome", 25, "Pleno");
for(var x in cliente){
  console.log(cliente[x]);
};

  • The same happens with Bigown, is showing the content within the function 'setAtualiza'. The ideal is that it stops before entering these functions, and return only the values that actually make sense. Thank you!

  • You can create a function "toString()" within the object itself and in this function you list only what interests you. Ex: toString : Function(){ Return "name: " + this.name + " age: "+this.age + " position: "+ this.cargo }. And at the time of displaying, you would invoke console.log(client.toString()).

4

Missed to call the function in the object itself, as a method. The way you did, it must be giving an error because the function does not exist outside the object. In code, I mean this:

cliente.setAtualiza("NovoNome", 25, "Pleno");
//  ˆ---- faltou isto!

Browser other questions tagged

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