File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments