Guest User

Untitled

a guest
Jan 22nd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. // Utilizando bind
  2.  
  3. const Persona = {
  4. nombre: "Luis",
  5. apellido: "García",
  6. idiomas: ["Español", "Inglés"],
  7. hablar: function() {
  8. this.idiomas.map(
  9. function(idioma) {
  10. console.log(this.nombre + " habla " + idioma);
  11. // Luis habla Español
  12. // Luis habla Inglés
  13. }.bind(this)
  14. );
  15. }
  16. };
  17. Persona.hablar();
  18.  
  19. // Utilizando arrow function
  20.  
  21. const Persona2 = {
  22. nombre: "Luis",
  23. apellido: "García",
  24. idiomas: ["Español", "Inglés"],
  25. hablar: function() {
  26. this.idiomas.map(
  27. (idioma)=> {
  28. console.log(this.nombre + " habla " + idioma);
  29. // Luis habla Español
  30. // Luis habla Inglés
  31. }
  32. );
  33. }
  34. };
  35. Persona2.hablar();
  36.  
  37. // Utilizando asignación de variables
  38.  
  39. const Persona3 = {
  40. nombre: "Luis",
  41. apellido: "García",
  42. idiomas: ["Español", "Inglés"],
  43. hablar: function() {
  44. // creamos una variable llamada por lo regular _self o _this la cual será una copia de this
  45. var _self = this
  46. this.idiomas.map(function (idioma) {
  47. console.log(_self.nombre + " habla " + idioma);
  48. // Luis habla Español
  49. // Luis habla Inglés
  50. });
  51. }
  52. };
  53.  
  54. Persona3.hablar();
Add Comment
Please, Sign In to add comment