Skip to content

Instantly share code, notes, and snippets.

@HashirHussain
Created September 7, 2018 07:31
Show Gist options
  • Save HashirHussain/540a98935037f4b2d8a351e19af468da to your computer and use it in GitHub Desktop.
Save HashirHussain/540a98935037f4b2d8a351e19af468da to your computer and use it in GitHub Desktop.
/*We know the purpose of classes in sense of programming concepts.
In JavaScript `class` is the makeover upon traditional prototype-inheritance.
*/
//ES5 Approach:
function Person(first, last) {
this.first = first;
this.last = last;
}
Person.prototype.firstName = function() {
console.log(this.first);
}
Person.prototype.lastName = function() {
console.log(this.last);
}
Person.prototype.fullName = fucntion() {
console.log(this.first + ' ' + this.last);
}
var me = new Person('Hashir', 'Hussain');
console.log(me.printName()); //Hashir Hussain
//ES6 Approach
class Person() {
constructor(first, last) {
this.first = first;
this.last = last;
}
firstName() {
console.log(this.first);
}
lastName() {
console.log(this.last);
}
fullName() {
console.log(this.first + ' ' + this.last);
}
}
var me = new Person('Hashir', 'Hussain');
console.log(me.fullName); //Hashir Hussain
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment