JSFiddle - React, Tailwind, and code Playground

by David Buzatto

JavaScript

// objetos em javascript.

// getter e/ou setter com declaração de objeto
function Pessoa( nome, sobrenome ) {
    this._nome = nome;
    this._sobrenome = sobrenome;
}

// precisa inserir no prototype
Pessoa.prototype.__defineGetter__( "nome", function() {
    return this._nome + " getter!";
});
    
Pessoa.prototype.__defineGetter__( "sobrenome", function() {
    return this._sobrenome + " getter!";
});
    
Pessoa.prototype.toString = function() {
    return this.nome + " " + this.sobrenome;
}

var pessoa = new Pessoa( "david", "buzatto" );
console.log( pessoa.nome );
console.log( pessoa.sobrenome );
console.log( pessoa.toString() );
console.log( pessoa );

// se for usando literal, pode adicionar direto (ECMA Script 5)
var instancia = { 
    _nome: "david", 
    _sobrenome: "buzatto",
    get nome() { 
        return this._nome + " getter!"
    },
    get sobrenome() {
        return this._sobrenome + " getter!"
    },
    toString: function() { // influencia no tipo! (comente para verificar!!!)
        return this.nome + " " + this.sobrenome;
    }
};
console.log( instancia.nome );
console.log( instancia.sobrenome );
console.log( instancia.toString() );
console.log( instancia );