Skip to content

Commit a8a8224

Browse files
committed
Add valueOf example
1 parent ec76379 commit a8a8224

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

examples/valueOf/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>valueOf Example</title>
6+
<link rel="stylesheet" href="http://code-warrior.github.io/styles/reset.css">
7+
<link rel="stylesheet" href="http://code-warrior.github.io/styles/base.css">
8+
</head>
9+
<body>
10+
<h1><code>valueOf</code> Example</h1>
11+
<p>Open your console to see this program’s output from the file <a href="valueOf.js">valueOf.js</a></p>
12+
<script src="valueOf.js"></script>
13+
</body>
14+
</html>

examples/valueOf/valueOf.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*jslint es6, single, devel, this */
2+
/*eslint no-console: ["error", { allow: ["log"] }] */
3+
4+
'use strict';
5+
6+
// Construct the Dog object
7+
function Dog(name, breed) {
8+
this.name = name;
9+
this.breed = breed;
10+
}
11+
12+
// Create a new Dog instance named dog
13+
let dog = new Dog(`Spot`, `mutt`);
14+
15+
// Retrieve the object itself
16+
console.log(dog.valueOf()); // Dog {name: "Spot", breed: "mutt"}
17+
18+
// Using prototypal inheritance, over-ride how valueOf behaves, returning the name
19+
// of the dog
20+
Dog.prototype.valueOf = function () {
21+
return this.name;
22+
};
23+
24+
console.log(dog.valueOf()); // Spot

0 commit comments

Comments
 (0)