Skip to content

Instantly share code, notes, and snippets.

@johnathan-sewell
Created April 29, 2012 20:00
Show Gist options
  • Save johnathan-sewell/2552964 to your computer and use it in GitHub Desktop.
Save johnathan-sewell/2552964 to your computer and use it in GitHub Desktop.
Example of a constructor function in JavaScript
//define a function
var BaristaFunction = function(name){
this.myName = name;
};
//define a method
BaristaFunction.prototype.sayHi = function(){
alert('Hi I\'m ' + this.myName);
};
//*** here's the interesting part ***
//create an object from the Barista using a constructor
var baristaObject = new BaristaFunction('Jose');
//test out our method
baristaObject.sayHi(); //alerts 'Hi I'm Jose'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment