Skip to content

Instantly share code, notes, and snippets.

@hliyan
Last active August 29, 2015 14:26
Show Gist options
  • Save hliyan/cf2736bccd3408ad2a30 to your computer and use it in GitHub Desktop.
Save hliyan/cf2736bccd3408ad2a30 to your computer and use it in GitHub Desktop.
javascript-001-prototypes-intro
  • Until ECMA6, JavaScript had no classes. It had (and still has) prototypes.
  • A class is like a mold from which you build an object. A prototype is itself an object.
// think of this as the class AND its constructor
var Person = function() {
  this.canTalk = true;
};

// this is a 'class method'
Person.prototype.greet = function() {
  if (this.canTalk) {
    console.log('Hi, I am ' + this.name);
  }
};

// instantiating
var person = new Person();

Further reading

  1. Mozilla reference - prototypes
  2. Mozilla reference - classes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment