Created
March 13, 2016 06:27
-
-
Save JasonDeving/78bb193d375d1b05564a to your computer and use it in GitHub Desktop.
Revealing prototype pattern
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 lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Revealing Prototype Pattern</title> | |
<script> | |
window.onload = function () { | |
var calc = new Calculator('Output'); | |
calc.add(2,2); | |
calc.subtract(2,2); | |
} | |
</script> | |
</head> | |
<body> | |
<div id="Output"></div> | |
<script> | |
// constructor | |
var Calculator = function(eq) { | |
// state goes here | |
this.eqCtl = document.getElementById(eq); | |
} | |
Calculator.prototype = function() { | |
//private members | |
var add = function(x,y) { | |
this.eqCtl.innerHTML = x + y; | |
}, | |
subtract = function (x,y) { | |
alert(x - y); | |
} | |
//public members | |
return { | |
add: add, | |
subtract: subtract | |
}; | |
}(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment