Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Utilizando bind
- const Persona = {
- nombre: "Luis",
- apellido: "García",
- idiomas: ["Español", "Inglés"],
- hablar: function() {
- this.idiomas.map(
- function(idioma) {
- console.log(this.nombre + " habla " + idioma);
- // Luis habla Español
- // Luis habla Inglés
- }.bind(this)
- );
- }
- };
- Persona.hablar();
- // Utilizando arrow function
- const Persona2 = {
- nombre: "Luis",
- apellido: "García",
- idiomas: ["Español", "Inglés"],
- hablar: function() {
- this.idiomas.map(
- (idioma)=> {
- console.log(this.nombre + " habla " + idioma);
- // Luis habla Español
- // Luis habla Inglés
- }
- );
- }
- };
- Persona2.hablar();
- // Utilizando asignación de variables
- const Persona3 = {
- nombre: "Luis",
- apellido: "García",
- idiomas: ["Español", "Inglés"],
- hablar: function() {
- // creamos una variable llamada por lo regular _self o _this la cual será una copia de this
- var _self = this
- this.idiomas.map(function (idioma) {
- console.log(_self.nombre + " habla " + idioma);
- // Luis habla Español
- // Luis habla Inglés
- });
- }
- };
- Persona3.hablar();
Add Comment
Please, Sign In to add comment