Last active
August 29, 2015 14:22
-
-
Save MacKentoch/0c4f4d34c1b7aa9a9949 to your computer and use it in GitHub Desktop.
define a class in javascript
This file contains 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
////////////////////////// | |
// definition | |
////////////////////////// | |
//class definition | |
function Programmer(name, favoriteLanguage){ | |
//object properties | |
this.name = name; | |
this.favoriteLanguage = favoriteLanguage; | |
} | |
// Add methods | |
Programmer.prototype.presentHimself = function(){ | |
console.info("Hi, my name is " + this.name + ", I love programming " + this.favoriteLanguage); | |
}; | |
////////////////////////// | |
// instance, use... | |
////////////////////////// | |
// Instantiate new objects | |
var JavascriptProgrammer = new Programmer("ECMA", "Javascript"); | |
// Invoke methods like this | |
JavascriptProgrammer.presentHimself(); // logs info in console : "Hi, my name is ECMA, I love programming Javascript" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment