V
V
Vladimir B.2014-07-02 10:59:12
JavaScript
Vladimir B., 2014-07-02 10:59:12

Inheritance in JS How to make a convenient appeal to the parent?

There is a self-written library for creating and inheriting classes.

// Создаём заготовку наследника
var nClass = function( ) { };
  nClass.prototype = parentClass.prototype;			

// Начинаем наследование
childClass.prototype = new nClass( );
childClass.prototype.constructor = childClass;

// Оставляем конструктор родителя
childClass.prototype.parentConstructor = parentClass;

// Вызов функции родителя ( с параметрами ), не указывая префикс
childClass.prototype.parentFunction = function( functionName, arguments )
{
  var arguments = ( arguments instanceof Array ) ? arguments : [ arguments ];

  if( this[ prefix + functionName ] instanceof Function )
  {
    return this[ prefix + functionName ].apply( this, arguments );
  }
  else
  {
    this.error( 'Функции "' + functionName + '" не существует!' );
  }
};

I want instead of any parentFunction , etc. make a parent object from which it would be possible to access the functionality already:
/*
child.parent.name
child.parent.scope
child.parent.prefix
child.parent.constructor
child.parent.function( 'name', [ arguments ] ); @return - результат выполнения функции
child.parent.parameter( 'name' ); @return - значение переменной/объект/т.д
*/

childClass.prototype.parent = {
  name: parentName, // Имя класса родителя
  scope: parentClass.classScope, // Область видимости родителя
  prefix: prefix, // Префикс для обращения к функциям родителя
  constructor: function( arguments ) 
  {
    var arguments = ( arguments instanceof Array ) ? arguments : [ arguments ];
          
    return parentClass.apply( childClass.prototype, arguments );
  },
  parameter: function( functionName )
  {
    if( childClass.prototype[ prefix + functionName ] !== undefined )
    {
      return childClass.prototype[ prefix + functionName ];
    }
  },
  function: function( functionName, arguments ) 
  { 
    var arguments = ( arguments instanceof Array ) ? arguments : [ arguments ];

    if( this[ prefix + functionName ] instanceof Function )
    {
      return this[ prefix + functionName ].apply( this, arguments );
    }
    else
    {
      this.error( 'Функции "' + functionName + '" не существует!' );
    }
  }
};

But of course, neither this[], nor any childClass.prototype[] will work here.
How to be?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ilya Milosevic, 2014-07-02
@ange007

Just in ExtJS, each class has a callParent method . This method relies on a property of the caller function object . Thus, callParent will know which method to call relative to the parent class.

R
Roman Zhak, 2014-07-02
@romanzhak

First you need to learn js to do such things :)
For example, the arguments collection must be taken from the activation object and cast to Array:

var fn = function( name ) {
  var sliced = Array.prototype.slice
    , args   = sliced.call( arguments, 1 );

Object.prototype.toString.call( args ); // => [object Array] 
}

K
KEKSOV, 2014-07-02
@KEKSOV

I think you will find several answers to your question here . You can also see how it's done in Ext.extend

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question