Skip to content

Instantly share code, notes, and snippets.

@juanmaguitar
Created August 3, 2017 08:14
Show Gist options
  • Save juanmaguitar/7cdf52ef00b51b07454652d9fed47376 to your computer and use it in GitHub Desktop.
Save juanmaguitar/7cdf52ef00b51b07454652d9fed47376 to your computer and use it in GitHub Desktop.
clases es2015

Clases ES2015

ES5

code

function Person( name ) {
  this.name = name
  console.log(this)
}

Person.prototype.talk = function() {
  console.log( "Hello " + this.name )
  return this
}

Person.talk = function() {
  console.log( "Hello " + this.name )
  return this
}

results in console

> var me = new Person('juanma')
Person {name: "juanma"}

> me.talk()
Hello juanma
Person {name: "juanma"}

> Person.talk()
Hello Person
ƒ Person( name ) {
  this.name = name
  console.log(this)
}

ES2015

class Person {
  constructor(name) {
    this.name = name
    console.log(this)
  }


  talk() {
    console.log( "Hello " + this.name )
    return this
  }

  static talk() {
    console.log( "Hello " + this.name )
    return this
  }

}
> var me = new Person('juanma')
Person {name: "juanma"}

> me.talk()
Hello juanma
Person {name: "juanma"}

> Person.talk()
Hello Person
class Person {
  constructor(name) {
    this.name = name
    console.log(this)
  }


  talk() {
    console.log( "Hello " + this.name )
    return this
  }

  static talk() {
    console.log( "Hello " + t…
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment