Created
April 29, 2012 20:00
-
-
Save johnathan-sewell/2552964 to your computer and use it in GitHub Desktop.
Example of a constructor function in JavaScript
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
//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