Skip to content

Instantly share code, notes, and snippets.

@JasonDeving
Created March 13, 2016 06:27
Show Gist options
  • Save JasonDeving/78bb193d375d1b05564a to your computer and use it in GitHub Desktop.
Save JasonDeving/78bb193d375d1b05564a to your computer and use it in GitHub Desktop.
Revealing prototype pattern
<!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