Error reading variable using Weakmap

Asked

Viewed 15 times

0

var person = {
    nome: 'Bruno',
    sobrenome: 'Coelho'
};
nomeMap = new WeakMap();
function nomeCompleto(){
    nomeMap.set(this, {
        nome: person.nome
    });
    nomeMap.set(this, {
        sobrenome: person.sobrenome
    });
    return console.log(nomeMap.get(this).nome + ' ' + nomeMap.get(this).sobrenome);
}
nomeCompleto();
I did not understand why he only reads the last name and not the variable name, how can I solve?

1 answer

0


The last instance is overriding the first. Use only one, this way:

var person = {
    nome: 'Bruno',
    sobrenome: 'Coelho'
};
nomeMap = new WeakMap();
function nomeCompleto(){
    nomeMap.set(this, {
        nome: person.nome,
        sobrenome: person.sobrenome
    });
    return console.log(nomeMap.get(this).nome + ' ' + nomeMap.get(this).sobrenome);
}
nomeCompleto();

Browser other questions tagged

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