1

皆さん、この質問について私を助けてくれませんか。

var Module = function ( options ) {
var settings = {
    selector: 'body',
    declaration: function () {
        return{
            init: function () {
                console.log( 'nodule initialize' );
            },
            destroy: function () {
                console.log( ' module destroyed' );
            }
        }
     }
   };
this.declaration = options.declaration || settings.declaration;
this.selector = options.selector || settings.selector;
};

var Registration = function ( options ) {
   this.selector = options.selector || **strong text**;
   this.declaration = options.declaration
}
app.utils.extend( Module, Registration );
var m_registration = new Registration( {
   declaration: function ( f ) {
      return {
        init: function () {
        },
        destroy: function () {
        }
    }
  }
} );

私の主な質問は、登録 のインスタンスを作成するときに渡される数量引数が必要ない場合、m_registration でModule.selectorプロパティを継承する方法です。

関数 app.utils.extend() の私の実現:

var app.utils.extend = function ( from, to ) {
        var F = function () {
        };
        F.prototype = from.prototype;
        to.prototype = new F();
        to.prototype.constructor = to;
        to.superclass = from.prototype;
    }

更新

これらの方法を使用している場合:

var Registration = function ( options ) {
       Module.call(this, { selector : options.selector });
       this.declaration = options.declaration
}

このクラスがどのインスタンスから拡張されたか、または継承が本当にわからない場合に、継承をどのように使用できるか。

4

2 に答える 2

1
var Module = function ( options ) {
    this.declaration = options.declaration;
    this.selector = options.selector;
}

Module.prototype = {
    selector: 'body',
    declaration: function () {
        return{
            init: function () {
                console.log( 'nodule initialize' );
            },
            destroy: function () {
                console.log( ' module destroyed' );
            }
        }
     }
   };

もう 1 つの方法は、継承するすべてのプロパティをプロパティに配置することですprototype。これは、JavaScriptエンジンが継承を処理する方法です

于 2013-06-15T11:28:27.607 に答える
1
var Registration = function ( options ) {
   Module.call(this, { selector : options.selector || **strong text**});
   this.declaration = options.declaration
}

**strong text**あなたの場合の意味がわかりません。でもこう呼ぶと

var Registration = function ( options ) {
       Module.call(this, { selector : options.selector });
       this.declaration = options.declaration
    }

または

var Registration = function ( options ) {
       Module.call(this, options );
       this.declaration = options.declaration
    }

あなたの場合m_registration.selector'body'

于 2013-06-15T11:14:11.740 に答える