Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 866e310

Browse files
Add toString example
1 parent f4242a5 commit 866e310

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

‎examples/toString/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>toString 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>toString</code> Example</h1>
11+
<p>Open your console to see this program’s output from the file <a href="toString.js">toString.js</a></p>
12+
<script src="toString.js"></script>
13+
</body>
14+
</html>

‎examples/toString/toString.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*jslint es6, single, devel, this */
2+
/*eslint no-console: ["error", { allow: ["log"] }] */
3+
4+
'use strict';
5+
6+
7+
let dog = {
8+
adoptable: true,
9+
vaccinated: false
10+
};
11+
12+
console.log(dog.toString()); // [object Object]
13+
14+
// Over-ride the inherited toString method in the prototype
15+
dog.toString = function () {
16+
let output = `This dog is `;
17+
18+
output += ((this.adoptable) ? `adoptable ` : `not adoptable `);
19+
output += ((this.vaccinated) ? `and vaccinated.` : `and not vaccinated.`);
20+
21+
return output;
22+
};
23+
24+
// This dog is adoptable and not vaccinated.
25+
console.log(dog.toString());
26+
27+
// true, because toString shadows the inherited prototype method for toString
28+
console.log(dog.hasOwnProperty(`toString`));

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /