<> Chapter 5 Inheritance 2016.12.24

In the classical languages (such as Java), inheritance (or extends) provides two usefulservices. First, it is a form of code reuse. If a new class is mostly similar to an existing class, you only have to specify the differences. Patterns of code reuse are extremely important because they have the potential to significantly reduce the cost of software development. The other benefit of classical inheritance is that it includes the specification of a system of types. This mostly frees the programmer from having to write explicit casting operations, which is a very good thing because when casting, the safety benefits of a type system are lost.
JavaScript, being a loosely typed language, never casts.
作为一门宽松类型的编程语言,JavaScript从来不cast。

JavaScript provides a much richer set of code reuse patterns. It can ape the classical pattern, but it also supports other patterns that are more expressive. The set of possible inheritance patterns in JavaScript is vast.
In classical languages, objects are instances of classes, and a class can inherit from another class. JavaScript is a prototypal language, which means that objects inherit directly from other objects.

Pseudoclassical#

this.prototype = {constructor: this};

Function.method('new', function () {
  // Create a new object that inherits from the constructor's prototype.
  var that = Object.create(this.prototype);
  // Invoke the constructor, binding –this- to the new object.
  var other = this.apply(that, arguments);
  // If its return value isn't an object, substitute the new object.
  return (typeof other === 'object' && other) || that; 
});

var Mammal = function (name) {
  this.name = name;
};
Mammal.prototype.get_name = function () {
  return this.name;
};
Mammal.prototype.says = function () {
  return this.saying || '';
};

var myMammal = new Mammal('Herb the Mammal');
var name = myMammal.get_name(); // 'Herb the Mammal'

var Cat = function (name) { 
  this.name = name; 
  this.saying = 'meow';
};
// Replace Cat.prototype with a new instance of Mammal
Cat.prototype = new Mammal();
// Augment the new prototype with purr and get_name methods.
Cat.prototype.purr = function (n) { 
  var i, s = '';
  for (i = 0; i < n; i += 1) { 
    if (s) {
      s += '-';
    }
    s += 'r';
  }
  return s;
};
Cat.prototype.get_name = function () {
  return this.says() + ' ' + this.name + ' ' + this.says();
};
var myCat = new Cat('Henrietta');
var says = myCat.says(); // 'meow'
var purr = myCat.purr(5); // 'r-r-r-r-r'
var name = myCat.get_name(); // 'meow Henrietta meow'

Function.method('inherits', function (Parent) {
  this.prototype = new Parent();
  return this;
});

var Cat = function (name) { 
  this.name = name; 
  this.saying = 'meow';
}.
  inherits(Mammal).
  method('purr', function (n) {
    var i, s = ''; 
    for (i = 0; i < n; i += 1) {
      if (s) { 
        s += '-';
      }
      s += 'r';
    }
    return s;
  }).
  method('get_name', function () {
    return this.says() + ' ' + this.name + ' ' + this.says();
  });

Object Specifiers#

var myObject = maker({ 
  first: f,
  last: l,
  middle: m,
  state: s,
  city: c
});

Prototypal#

var myMammal = {
  name : 'Herb the Mammal',
  get_name : function () {
    return this.name; 
  },
  says : function () {
    return this.saying || '';
  }
};

var myCat = Object.create(myMammal); 
myCat.name = 'Henrietta'; 
myCat.saying = 'meow'; 
myCat.purr = function (n) {
  var i, s = ''; 
  for (i = 0; i < n; i += 1) {
    if (s) { 
      s += '-';
    }
    s += 'r';
  }
  return s;
};
myCat.get_name = function () {
  return this.says() + ' ' + this.name + ' ' + this.says();
};

var block = function () {
  // Remember the current scope. Make a new scope that includes everything from the current one.
  var oldScope = scope; 
  scope = Object.create(scope);
  // Advance past the left curly brace. 
  advance('{');
  // Parse using the new scope. 
  parse(scope);
  // Advance past the right curly brace and discard the new scope, restoring the old one.
  advance('}');
  scope = oldScope; 
};

Functional#

var constructor = function (spec, my) {
  var that, other private instance variables;
  my = my || {};
  Add shared variables and functions to my
  that = a new object;
  Add privileged methods to that
  return that;
};

Parts#

你可能感兴趣的:(<> Chapter 5 Inheritance 2016.12.24)