Skip to content

Instantly share code, notes, and snippets.

@chadsansing
Created May 26, 2015 13:05
Show Gist options
  • Save chadsansing/ff00e262c0d462f7cba0 to your computer and use it in GitHub Desktop.
Save chadsansing/ff00e262c0d462f7cba0 to your computer and use it in GitHub Desktop.
Example of OOP for class
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script>
function randomObject() {
function Dessert(name, taste, texture) {
this.name = name;
this.taste = taste;
this.texture = texture;
}
var chocolatePudding = new Dessert("Chocolate pudding", "rich", "gooey");
var cheeseCake = new Dessert("Cheese cake", "tart", "smooth");
var oatmealRaisinCookies = new Dessert("Oatmeal-raisin cookies", "sweet", "chewy");
var desserts = [chocolatePudding, cheeseCake, oatmealRaisinCookies];
this.desserts = desserts[Math.random(Math.round()*(desserts.length-1))];
this.getDessertsName = this.desserts.name;
this.getDessertsTaste = this.desserts.taste;
this.getDessertsTexture = this.desserts.texture;
document.getElementById("myObject").innerHTML = "<div>" + this.getDessertsName + " is/are " + this.getDessertsTaste + " and " + this.getDessertsTexture + ".</div>"
}
</script>
</head>
<body>
<p><button onClick="randomObject()">Click me!</button></p>
<div id="myObject"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment