-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththisExercise.html
83 lines (78 loc) · 2.79 KB
/
thisExercise.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<html>
<head>
<title>This Exercise</title>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css">
<style>
body {
font-size: 16px;
}
button {
background: #3B5998;
border: none;
border-radius: 4px;
color: #fff;
cursor:pointer;
font-size: 18px;
padding: 1em;
}
pre {
margin: 2em 0;
}
.wrapper {
margin: 0 auto;
max-width: 1120px;
padding: 2rem;
}
</style>
</head>
<body>
<div class="wrapper">
<button id="button" type="button">Click Me and check the console!!</button>
<pre>
var user = {
data:[
{name:"C. Damnage", age:37},
{name:"T. Jones", age:43},
{name:"D. West", age:40},
{name:"K. Slayfield", age:39}
],
clickHandler:function (event) {
// Math.floor(Math.random() * (max - min + 1)) + min
var randomNum = Math.floor(Math.random() * (3 - 0 + 1) - 0);
// This line is printing a random person's name and age from the data array
console.log(this.data[randomNum].name + " " + this.data[randomNum].age);
}
};
document.getElementById('button').onclick = user.clickHandler;
</pre>
</div>
<footer>
<div class="wrapper">
<p>
Source: <a href="http://javascriptissexy.com/understand-javascripts-this-with-clarity-and-master-it/">Understand Javascripts this with Clarity and Master It</a>
</p>
<p>
Followup: <a href="https://bocoup.com/blog/the-many-faces-of-functions-in-javascript">The Many Faces of Functions in Javascript</a>
</p>
</div>
</footer>
<script>
// We have a simple object with a clickHandler method that we want to use when a button on the page is clicked
var user = {
data:[
{name:"C. Damnage", age:37},
{name:"T. Jones", age:43},
{name:"D. West", age:40},
{name:"K. Slayfield", age:39}
],
clickHandler:function (event) {
// Math.floor(Math.random() * (max - min + 1)) + min
var randomNum = Math.floor(Math.random() * (3 - 0 + 1) - 0);
// This line is printing a random person's name and age from the data array
console.log(this.data[randomNum].name + " " + this.data[randomNum].age);
}
};
document.getElementById('button').onclick = user.clickHandler;
</script>
</body>
</html>