Улучшение простого наследования JavaScript

Джон Резиг (известный специалист по jQuery) предлагает краткую реализацию простого наследования JavaScript. Его подход вдохновил меня на попытку улучшить ситуацию еще больше. Я переписал исходную функцию Resig Class.extend, добавив следующие преимущества:

  • Производительность — меньше накладных расходов при определении класса, построении объекта и вызовах методов базового класса.

  • Гибкость — оптимизирован для новых браузеров, совместимых с ECMAScript 5 (например, Chrome), но обеспечивает эквивалентную «прокладку» для старых браузеров (например, IE6).

  • Совместимость — проверяется в строгом режиме и обеспечивает лучшую совместимость инструментов (например, комментарии VSDoc/JSDoc, Visual Studio IntelliSense и т. д.)

  • Простота: вам не нужно быть «ниндзя», чтобы понять исходный код (это еще проще, если вы потеряете функции ECMAScript 5).

  • Надежность — проходит больше модульных тестов "углового случая" (например, переопределение toString в IE).

Поскольку это кажется слишком хорошим, чтобы быть правдой, я хочу убедиться, что моя логика не имеет каких-либо фундаментальных недостатков или ошибок, и посмотреть, может ли кто-нибудь предложить улучшения или опровергнуть код. На этом я представляю функцию classify:

function classify(base, properties)
{
    /// <summary>Creates a type (i.e. class) that supports prototype-chaining (i.e. inheritance).</summary>
    /// <param name="base" type="Function" optional="true">The base class to extend.</param>
    /// <param name="properties" type="Object" optional="true">The properties of the class, including its constructor and members.</param>
    /// <returns type="Function">The class.</returns>

    // quick-and-dirty method overloading
    properties = (typeof(base) === "object") ? base : properties || {};
    base = (typeof(base) === "function") ? base : Object;

    var basePrototype = base.prototype;
    var derivedPrototype;

    if (Object.create)
    {
        // allow newer browsers to leverage ECMAScript 5 features
        var propertyNames = Object.getOwnPropertyNames(properties);
        var propertyDescriptors = {};

        for (var i = 0, p; p = propertyNames[i]; i++)
            propertyDescriptors[p] = Object.getOwnPropertyDescriptor(properties, p);

        derivedPrototype = Object.create(basePrototype, propertyDescriptors);
    }
    else
    {
        // provide "shim" for older browsers
        var baseType = function() {};
        baseType.prototype = basePrototype;
        derivedPrototype = new baseType;

        // add enumerable properties
        for (var p in properties)
            if (properties.hasOwnProperty(p))
                derivedPrototype[p] = properties[p];

        // add non-enumerable properties (see https://developer.mozilla.org/en/ECMAScript_DontEnum_attribute)
        if (!{ constructor: true }.propertyIsEnumerable("constructor"))
            for (var i = 0, a = [ "constructor", "hasOwnProperty", "isPrototypeOf", "propertyIsEnumerable", "toLocaleString", "toString", "valueOf" ], p; p = a[i]; i++)
                if (properties.hasOwnProperty(p))
                    derivedPrototype[p] = properties[p];
    }

    // build the class
    var derived = properties.hasOwnProperty("constructor") ? properties.constructor : function() { base.apply(this, arguments); };
    derived.prototype = derivedPrototype;
    derived.prototype.constructor = derived;
    derived.prototype.base = derived.base = basePrototype;

    return derived;
}

И использование почти идентично Resig, за исключением имени конструктора (constructor против init) и синтаксиса для вызовов методов базового класса.

/* Example 1: Define a minimal class */
var Minimal = classify();

/* Example 2a: Define a "plain old" class (without using the classify function) */
var Class = function()
{
    this.name = "John";
};

Class.prototype.count = function()
{
    return this.name + ": One. Two. Three.";
};

/* Example 2b: Define a derived class that extends a "plain old" base class */
var SpanishClass = classify(Class,
{
    constructor: function()
    {
        this.name = "Juan";
    },
    count: function()
    {
        return this.name + ": Uno. Dos. Tres.";
    }
});

/* Example 3: Define a Person class that extends Object by default */
var Person = classify(
{
    constructor: function(name, isQuiet)
    {
        this.name = name;
        this.isQuiet = isQuiet;
    },
    canSing: function()
    {
        return !this.isQuiet;
    },
    sing: function()
    {
        return this.canSing() ? "Figaro!" : "Shh!";
    },
    toString: function()
    {
        return "Hello, " + this.name + "!";
    }
});

/* Example 4: Define a Ninja class that extends Person */
var Ninja = classify(Person,
{
    constructor: function(name, skillLevel)
    {
        Ninja.base.constructor.call(this, name, true);
        this.skillLevel = skillLevel;
    },
    canSing: function()
    {
        return Ninja.base.canSing.call(this) || this.skillLevel > 200;
    },
    attack: function()
    {
        return "Chop!";
    }
});

/* Example 4: Define an ExtremeNinja class that extends Ninja that extends Person */
var ExtremeNinja = classify(Ninja,
{
    attack: function()
    {
        return "Chop! Chop!";
    },
    backflip: function()
    {
        this.skillLevel++;
        return "Woosh!";
    }
});

var m = new Minimal();
var c = new Class();
var s = new SpanishClass();
var p = new Person("Mary", false);
var n = new Ninja("John", 100);
var e = new ExtremeNinja("World", 200);

И вот мои тесты QUnit, которые все проходят:

equals(m instanceof Object && m instanceof Minimal && m.constructor === Minimal, true);
equals(c instanceof Object && c instanceof Class && c.constructor === Class, true);
equals(s instanceof Object && s instanceof Class && s instanceof SpanishClass && s.constructor === SpanishClass, true);
equals(p instanceof Object && p instanceof Person && p.constructor === Person, true);
equals(n instanceof Object && n instanceof Person && n instanceof Ninja && n.constructor === Ninja, true);
equals(e instanceof Object && e instanceof Person && e instanceof Ninja && e instanceof ExtremeNinja && e.constructor === ExtremeNinja, true);

equals(c.count(), "John: One. Two. Three.");
equals(s.count(), "Juan: Uno. Dos. Tres.");

equals(p.isQuiet, false);
equals(p.canSing(), true);
equals(p.sing(), "Figaro!");

equals(n.isQuiet, true);
equals(n.skillLevel, 100);
equals(n.canSing(), false);
equals(n.sing(), "Shh!");
equals(n.attack(), "Chop!");

equals(e.isQuiet, true);
equals(e.skillLevel, 200);
equals(e.canSing(), false);
equals(e.sing(), "Shh!");
equals(e.attack(), "Chop! Chop!");
equals(e.backflip(), "Woosh!");
equals(e.skillLevel, 201);
equals(e.canSing(), true);
equals(e.sing(), "Figaro!");
equals(e.toString(), "Hello, World!");

Кто-нибудь не видит ничего плохого в моем подходе по сравнению с первоначальным подходом Джона Резига? Предложения и отзывы приветствуются!

ПРИМЕЧАНИЕ. Приведенный выше код был значительно изменен с тех пор, как я впервые опубликовал этот вопрос. Вышеуказанное представляет последнюю версию. Чтобы узнать, как он развивался, проверьте историю изменений.


person Community    schedule 24.03.2010    source источник
comment
Я бы рекомендовал Object.create и traitsjs. Наследование не годится в javascript, используйте композицию объектов   -  person Raynos    schedule 28.07.2011
comment
Может я просто еще не привык, но от синтаксиса трейтов голова идет кругом. Я думаю, что я пройду, пока он не наберет последователей ...   -  person Will    schedule 28.07.2011


Ответы (3)


Некоторое время назад я рассмотрел несколько объектных систем для JS и даже реализовал несколько собственных, например class.js (версия ES5) и proto.js.

Причина, по которой я никогда их не использовал: в итоге вы напишете такое же количество кода. Показательный пример: пример Resig's Ninja (добавлены только некоторые пробелы):

var Person = Class.extend({
    init: function(isDancing) {
        this.dancing = isDancing;
    },

    dance: function() {
        return this.dancing;
    }
});

var Ninja = Person.extend({
    init: function() {
        this._super(false);
    },

    swingSword: function() {
        return true;
    }
});

19 строк, 264 байта.

Стандартный JS с Object.create() (это функция ECMAScript 5, но для наших целей ее можно заменить пользовательской ES3 clone()):

function Person(isDancing) {
    this.dancing = isDancing;
}

Person.prototype.dance = function() {
    return this.dancing;
};

function Ninja() {
    Person.call(this, false);
}

Ninja.prototype = Object.create(Person.prototype);

Ninja.prototype.swingSword = function() {
    return true;
};

17 строк, 282 байта. Имо, дополнительные байты на самом деле не являются дополнительной сложностью отдельной объектной системы. Достаточно просто сделать стандартный пример короче, добавив несколько пользовательских функций, но опять же: оно того не стоит.

person Christoph    schedule 24.03.2010
comment
Раньше я думал так же, как и вы, но теперь вынужден не согласиться. По сути, все, что мы с Джоном Резигом сделали, это создали единственный метод (extend), который связывает точное поведение объекта/прототипа, которое вы имеете в приведенном выше примере (т.е. это не новая объектная система). Единственное отличие состоит в том, что синтаксис короче, точнее и менее подвержен ошибкам при использовании метода расширения. - person Will; 25.03.2010
comment
Поразмыслив над этим, я согласен с тем, что использование стандартного синтаксиса подключения так же коротко, как и использование синтаксиса расширения. Я все еще думаю, что последний намного чище, менее подвержен ошибкам и является скорее соглашением, чем подходом к настройке, если это имеет смысл. - person Will; 25.03.2010
comment
Я часами ломал голову, пытаясь понять подход Резига, но так и не смог его понять. Это НАСТОЛЬКО прямолинейно. Спасибо!! - person Big McLargeHuge; 26.07.2012
comment
Подождите минутку... как вы определяете пользовательский конструктор? например установить Ninja.strength = function () { Math.random(); } при определении нового ниндзя? - person Big McLargeHuge; 26.07.2012

Не так быстро. Это просто не работает.

Рассмотреть возможность:

var p = new Person(true);
alert("p.dance()? " + p.dance()); => true

var n = new Ninja();
alert("n.dance()? " + n.dance()); => false
n.dancing = true;
alert("n.dance()? " + n.dance()); => false

base — это просто еще один объект, инициализированный членами по умолчанию, которые заставили вас думать, что он работает.

РЕДАКТИРОВАТЬ: для справки, вот моя собственная (хотя и более подробная) реализация Java, такая как наследование в Javascript, созданная в 2006 году, когда меня вдохновил Base.js Дина Эдварда (и я согласен с ним, когда он говорит, что версия Джона — это просто переписанный его Base.js). Вы можете увидеть его в действии (и выполнить пошаговую отладку в Firebug) здесь.

/**
 * A function that does nothing: to be used when resetting callback handlers.
 * @final
 */
EMPTY_FUNCTION = function()
{
  // does nothing.
}

var Class =
{
  /**
   * Defines a new class from the specified instance prototype and class
   * prototype.
   *
   * @param {Object} instancePrototype the object literal used to define the
   * member variables and member functions of the instances of the class
   * being defined.
   * @param {Object} classPrototype the object literal used to define the
   * static member variables and member functions of the class being
   * defined.
   *
   * @return {Function} the newly defined class.
   */
  define: function(instancePrototype, classPrototype)
  {
    /* This is the constructor function for the class being defined */
    var base = function()
    {
      if (!this.__prototype_chaining 
          && base.prototype.initialize instanceof Function)
        base.prototype.initialize.apply(this, arguments);
    }

    base.prototype = instancePrototype || {};

    if (!base.prototype.initialize)
      base.prototype.initialize = EMPTY_FUNCTION;

    for (var property in classPrototype)
    {
      if (property == 'initialize')
        continue;

      base[property] = classPrototype[property];
    }

    if (classPrototype && (classPrototype.initialize instanceof Function))
      classPrototype.initialize.apply(base);

    function augment(method, derivedPrototype, basePrototype)
    {
      if (  (method == 'initialize')
          &&(basePrototype[method].length == 0))
      {
        return function()
        {
          basePrototype[method].apply(this);
          derivedPrototype[method].apply(this, arguments);
        }
      }

      return function()
      {
        this.base = function()
                    {
                      return basePrototype[method].apply(this, arguments);
                    };

        return derivedPrototype[method].apply(this, arguments);
        delete this.base;
      }
    }

    /**
     * Provides the definition of a new class that extends the specified
     * <code>parent</code> class.
     *
     * @param {Function} parent the class to be extended.
     * @param {Object} instancePrototype the object literal used to define
     * the member variables and member functions of the instances of the
     * class being defined.
     * @param {Object} classPrototype the object literal used to define the
     * static member variables and member functions of the class being
     * defined.
     *
     * @return {Function} the newly defined class.
     */
    function extend(parent, instancePrototype, classPrototype)
    {
      var derived = function()
      {
        if (!this.__prototype_chaining
            && derived.prototype.initialize instanceof Function)
          derived.prototype.initialize.apply(this, arguments);
      }

      parent.prototype.__prototype_chaining = true;

      derived.prototype = new parent();

      delete parent.prototype.__prototype_chaining;

      for (var property in instancePrototype)
      {
        if (  (instancePrototype[property] instanceof Function)
            &&(parent.prototype[property] instanceof Function))
        {
            derived.prototype[property] = augment(property, instancePrototype, parent.prototype);
        }
        else
          derived.prototype[property] = instancePrototype[property];
      }

      derived.extend =  function(instancePrototype, classPrototype)
                        {
                          return extend(derived, instancePrototype, classPrototype);
                        }

      for (var property in classPrototype)
      {
        if (property == 'initialize')
          continue;

        derived[property] = classPrototype[property];
      }

      if (classPrototype && (classPrototype.initialize instanceof Function))
        classPrototype.initialize.apply(derived);

      return derived;
    }

    base.extend = function(instancePrototype, classPrototype)
                  {
                    return extend(base, instancePrototype, classPrototype);
                  }
    return base;
  }
}

И вот как вы его используете:

var Base = Class.define(
{
  initialize: function(value) // Java constructor equivalent
  {
    this.property = value;
  }, 

  property: undefined, // member variable

  getProperty: function() // member variable accessor
  {
    return this.property;
  }, 

  foo: function()
  {
    alert('inside Base.foo');
    // do something
  }, 

  bar: function()
  {
    alert('inside Base.bar');
    // do something else
  }
}, 
{
  initialize: function() // Java static initializer equivalent
  {
    this.property = 'Base';
  },

  property: undefined, // static member variables can have the same
                                 // name as non static member variables

  getProperty: function() // static member functions can have the same
  {                                 // name as non static member functions
    return this.property;
  }
});

var Derived = Base.extend(
{
  initialize: function()
  {
    this.base('derived'); // chain with parent class's constructor
  }, 

  property: undefined, 

  getProperty: function()
  {
    return this.property;
  }, 

  foo: function() // override foo
  {
    alert('inside Derived.foo');
    this.base(); // call parent class implementation of foo
    // do some more treatments
  }
}, 
{
  initialize: function()
  {
    this.property = 'Derived';
  }, 

  property: undefined, 

  getProperty: function()
  {
    return this.property;
  }
});

var b = new Base('base');
alert('b instanceof Base returned: ' + (b instanceof Base));
alert('b.getProperty() returned: ' + b.getProperty());
alert('Base.getProperty() returned: ' + Base.getProperty());

b.foo();
b.bar();

var d = new Derived('derived');
alert('d instanceof Base returned: ' + (d instanceof Base));
alert('d instanceof Derived returned: ' + (d instanceof Derived));
alert('d.getProperty() returned: ' + d.getProperty());  
alert('Derived.getProperty() returned: ' + Derived.getProperty());

d.foo();
d.bar();
person Gregory Pakosz    schedule 24.03.2010
comment
Дарн, спасибо, что указали на этот главный недостаток. Я попробую еще раз, но, вероятно, закончу прямо на оригинальной функции Джона Резига. - person Will; 25.03.2010
comment
Конечно, функция Джона в полном порядке (опять же, он должен был отдать должное Дину Эдвардсу). В любом случае, попробуйте еще раз, как я тогда: это часть веселья, и понимание внутренней работы языка сделает вас (почувствуете) лучшим программистом. Интересно, что я никогда не использовал свою реализацию, это было просто ради нее :) Также я не вижу смысла пытаться сжать максимальное количество логики в минимальное количество кода: конечно, моя версия многословна, но каждый раз, когда я возвращаюсь к чтению, я понимаю, что происходит. - person Gregory Pakosz; 25.03.2010
comment
Я верю, что теперь все работает. Я внес небольшое изменение в вызовы базовых методов, чтобы использовать синтаксис base.method.call(this), который устраняет проблему, о которой вы сообщили. Видите ли вы другие проблемы с реализацией? Я не уверен, что это бессмысленное занятие. Я считаю, что одна из причин, по которой большинство разработчиков уклоняются от наследования JavaScript, заключается в том, что понимание реализации связано с черной магией или уродливым синтаксисом наследования, к которому они вынуждены прибегать. Я считаю, что это помогает решить обе проблемы (конечно, при условии, что это правильно). - person Will; 25.03.2010
comment
Понимание внутренностей не бессмысленно. Однако я всегда считал, что в конкуренции между jQuery, Mootools и т. д. нет смысла; но опять же, я никогда не сталкивался с замедлением загрузки страниц в моих домашних проектах, вызванным раздутыми скриптами. Тогда я недостаточно эксперт (хотя я считаю, что сделал хорошую домашнюю работу по реализации Java, такой как наследование) в Javascript, чтобы решить, что это правильный путь: такие эксперты, как Дуглас Крокфорд, утверждают, что нужно стремиться полностью принять прототипизм и освободить себя выйти за рамки классической модели - person Gregory Pakosz; 25.03.2010

Это настолько просто, насколько это возможно. Он был взят с сайта http://www.sitepoint.com/javascript-inheritance/.

// copyPrototype is used to do a form of inheritance.  See http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/#
// Example:
//    function Bug() { this.legs = 6; }
//    Insect.prototype.getInfo = function() { return "a general insect"; }
//    Insect.prototype.report = function() { return "I have " + this.legs + " legs"; }
//    function Millipede() { this.legs = "a lot of"; }
//    copyPrototype(Millipede, Bug);  /* Copy the prototype functions from Bug into Millipede */
//    Millipede.prototype.getInfo = function() { return "please don't confuse me with a centipede"; } /* ''Override" getInfo() */
function copyPrototype(descendant, parent) {
  var sConstructor = parent.toString();
  var aMatch = sConstructor.match(/\s*function (.*)\(/);
  if (aMatch != null) { descendant.prototype[aMatch[1]] = parent; }
  for (var m in parent.prototype) {

    descendant.prototype[m] = parent.prototype[m];
  }
};
person John Fisher    schedule 24.03.2010
comment
Это просто, хорошо, но не так полезно (без базы/супердоступа) и не красиво, ИМО. - person Will; 25.03.2010
comment
@Will: вы можете получить доступ к родительским методам. Проверьте ссылку для получения дополнительных объяснений. - person John Fisher; 25.03.2010
comment
@JohnFisher Я думаю, вы имели в виду Bug.prototype, а не Insert.prototype в комментариях к коду. - person Larry Battle; 06.08.2012
comment
@LarryBattle: я ничего не имел в виду, так как я не создавал код. Это с сайта. - person John Fisher; 06.08.2012