Created
November 14, 2018 01:38
-
-
Save emartini/05530eab7284f5700917d63095fb4faa to your computer and use it in GitHub Desktop.
3 formas de crear una clase en JS (ES6)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Esto es un objeto en JS: | |
const person = { | |
name: 'Chris', | |
saludo: function() { | |
console.log('¡Hola!, Soy '+ this.name + '.'); | |
}, | |
saludo2: () => { | |
// no funciono con arrow functions | |
console.log('¡Hola!, Soy '+ this.name + '.'); | |
} | |
} | |
const Person = function ({ name }) { | |
this.name = name; | |
this.saludo = function() { | |
console.log('¡Hola!, Soy '+ this.name + '.'); | |
} | |
this.saludo2 = () => { | |
console.log('¡Hola!, Soy '+ this.name + '.'); | |
} | |
}; | |
const PersonV2 = function ({ name }) { | |
this.name = name; | |
}; | |
PersonV2.prototype.saludo = function() { | |
console.log('¡Hola!, Soy '+ this.name + '.'); | |
}; | |
// Tampoco funciona | |
PersonV2.prototype.saludo2 = () => { | |
console.log('¡Hola!, Soy '+ this.name + '.'); | |
}; | |
class PersonV3 { | |
constructor({name}) { | |
this.name = name; | |
} | |
saludo(){ | |
console.log('¡Hola!, Soy '+ this.name + '.'); | |
} | |
} | |
// la palabra clave `new` espera como argumento un constructor | |
const person1 = new Person({ name: 'Pablo' }); | |
const person2 = new PersonV2({ name: 'Matías' }); | |
const person3 = new PersonV3({ name: 'Ignacio' }); | |
person.saludo(); | |
person.saludo2(); | |
person1.saludo(); | |
person1.saludo2(); | |
person2.saludo(); | |
person2.saludo2(); | |
person3.saludo(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment