Eloquent JavaScript 8 methods

by Joe LeMonnier

HTML

<script src="https://getfirebug.com/firebug-lite-debug.js"></script>

JavaScript

var rabbit = {};
rabbit.speako = function(line) {
  document .write("The rabbit says '" + line + "'");
};

rabbit.speako("I'm alive.");
// → The rabbit says 'I'm alive.


function speak(line) {
  document .write("<br>The " + this.type + " rabbit says '" +
              line + "'");
}
var whiteRabbit = {type: "white", speak:speak};
var fatRabbit = {type: "fat", speak: speak};

whiteRabbit.speak("<br>Oh my ears and whiskers, " +
                  "how late it's getting!");
// → The white rabbit says 'Oh my ears and whiskers, how
//   late it's getting!'
fatRabbit.speak("I could sure use a carrot right now.");
// → The fat rabbit says 'I could sure use a carrot
//   right now.

var empty = 12;
document.write('<br>',empty.toString);
// → function toString(){…}
document.write('<br>',empty.toString());
// → [object Object]

console.log(Object.getPrototypeOf({empty}) ==
            Object.prototype);
// → true
console.log(Object.getPrototypeOf(Object.prototype));
// → null
//https://getfirebug.com/firebug-lite-debug.js


var map = {};
function storePhi(event, phi) {
  map[event] = phi;
}

storePhi("pizza", 0.069);
storePhi("touched tree", -0.081);

console.log(map); 
document.write('<br>New',map.pizza);