Created
April 23, 2016 04:46
-
-
Save joechan3/b8365cbba60803cd30c1d8c4c3e5301d to your computer and use it in GitHub Desktop.
Prototype Example from Lynda: JavaScript Essential Training
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title></title> | |
</head> | |
<body> | |
<div id="mainContent"> | |
<h1>Prototype Example</h1> | |
</div> | |
<script src="script.js"></script> | |
</body> | |
</html> |
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
// Simple prototype example | |
function Player(n,s,r) { | |
this.name = n; | |
this.score = s; | |
this.rank = r; | |
} | |
Player.prototype.logInfo = function() { | |
console.log("I am:" , this.name); | |
} | |
Player.prototype.promote = function() { | |
this.rank++; | |
console.log("My new rank is: " , this.rank); | |
} | |
var fred = new Player("Fred",10000,5); | |
fred.logInfo(); | |
fred.promote(); | |
var bob = new Player("Bob",50,1); | |
bob.logInfo(); | |
bob.promote(); | |
var jane = new Player("Jane",50000,10); | |
jane.logInfo(); | |
jane.promote(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment