Guest User

Untitled

a guest
Dec 9th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. function Foo(who) {
  2. this.me = who;
  3. }
  4.  
  5. Foo.prototype.identify = function() {
  6. return "I am " + this.me;
  7. };
  8.  
  9. function Bar(who) {
  10. Foo.call(this,"Bar:" + who);
  11. }
  12.  
  13. Bar.prototype = Object.create(Foo.prototype);
  14. Bar.prototype.constructor = Bar; // "fixes" the delegated `constructor` reference
  15.  
  16. Bar.prototype.speak = function() {
  17. alert("Hello, " + this.identify() + ".");
  18. };
  19.  
  20. var b1 = new Bar("b1");
  21. var b2 = new Bar("b2");
  22.  
  23. b1.speak(); // alerts: "Hello, I am Bar:b1."
  24. b2.speak(); // alerts: "Hello, I am Bar:b2."
  25.  
  26. // some type introspection
  27. b1 instanceof Bar; // true
  28. b2 instanceof Bar; // true
  29. b1 instanceof Foo; // true
  30. b2 instanceof Foo; // true
  31. Bar.prototype instanceof Foo; // true
  32. Bar.prototype.isPrototypeOf(b1); // true
  33. Bar.prototype.isPrototypeOf(b2); // true
  34. Foo.prototype.isPrototypeOf(b1); // true
  35. Foo.prototype.isPrototypeOf(b2); // true
  36. Foo.prototype.isPrototypeOf(Bar.prototype); // true
  37. Object.getPrototypeOf(b1) === Bar.prototype; // true
  38. Object.getPrototypeOf(b2) === Bar.prototype; // true
  39. Object.getPrototypeOf(Bar.prototype) === Foo.prototype; // true
Add Comment
Please, Sign In to add comment