JS Prototype Chain

上一篇 / 下一篇  2015-08-15 11:14:00 / 个人分类:JavaScript

Up the Food-I-mean-Prototype Chain

A penguin is an animal and an emperor penguin is a penguin. Are emperor penguins animals too? Of course!

The "prototype chain" in JavaScript. knows this as well. If JavaScript. encounters something it can't find in the current class's methods or properties, it looks up the prototype chain to see if it's defined in a class that it inherits from. This keeps going upwards until it stops all the way at the top: the mighty Object.prototype (more on this later). By default, all classes inherit directly from Object, unless we change the class's prototype, like we've been doing for Penguin and Emperor

Instructions

Let's see how going up the prototype chain works! We've defined some classes and inheritance patterns: Emperorinherits from Penguin which inherits from Animal. We've also created an instance of the Emperor class.

Without modifying anything other than lines 22-24, complete the console.logstatements to print the appropriate responses.

Remember how the prototype chain works: if a property is not defined for a class, this class's prototype chain will be traversed upwards until one is found (or not) in a parent (higher) class.


Sample Codes:

// original classes

function Animal(name, numLegs) {

    this.name = name;

    this.numLegs = numLegs;

    this.isAlive = true;

}

function Penguin(name) {

    this.name = name;

    this.numLegs = 2;

}

function Emperor(name) {

    this.name = name;

    this.saying = "Waddle waddle";

}


// set up the prototype chain

Penguin.prototype = new Animal();

Emperor.prototype = new Penguin();


var myEmperor = new Emperor("Jules");


console.log( myEmperor.saying ); // should print "Waddle waddle"

console.log( myEmperor.numLegs); // should print 2

console.log( myEmperor.isAlive ); // should print true



TAG:

wilber.shinobi的个人空间 引用 删除 wilber.shinobi   /   2015-08-15 12:05:27
Accessing Private Variables
Although we cannot directly access private variables from outside the class, there is a way to get around this. We can define a public method that returns the value of a private variable.

Instructions
Here we have included similar code from last time, but here we have added a method getBalance. Modify getBalance so that it returns bankBalance.

Then on line 17, create a new variable named myBalance and set its value to John's bank balance. You can do this by calling your newly-defined getBalance method for john. Then print myBalance.

Line 14 should still print undefined!

Sample Codes:
function Person(first,last,age) {
   this.firstname = first;
   this.lastname = last;
   this.age = age;
   var bankBalance = 7500;
  
   this.getBalance = function() {
      // your code should return the bankBalance
      return bankBalance;
   };
}

var john = new Person('John','Smith',30);
console.log(john.bankBalance);

// create a new variable myBalance that calls getBalance()

var myBalance = john.getBalance();
console.log(myBalance);

Result:
undefined
7500
wilber.shinobi的个人空间 引用 删除 wilber.shinobi   /   2015-08-15 11:17:06
Remember how the prototype chain works:
if a property is not defined for a class, this class's prototype chain will be traversed upwards until one is found (or not) in a parent (higher) class.
 

评分:0

我来说两句

Open Toolbar